├── test_php2python.py
├── setup.py
├── .gitignore
├── LICENSE
├── php2python.py
└── README.md
/test_php2python.py:
--------------------------------------------------------------------------------
1 | import unittest
2 | import php2python as p2py
3 |
4 |
5 | class Test(unittest.TestCase):
6 |
7 | def test_checkdate(self):
8 | pass
9 |
10 |
11 | if __name__ == '__main__':
12 | unittest.main()
13 |
--------------------------------------------------------------------------------
/setup.py:
--------------------------------------------------------------------------------
1 | from setuptools import setup
2 |
3 | setup(name='php2python',
4 | version='0.0.1',
5 | description='Python alternatives for PHP functions',
6 | url='https://github.com/jxlwqq/php2python',
7 | author='jxlwqq',
8 | author_email='jxlwqq@gmail.com',
9 | license='MIT',
10 | python_requires='>=3',
11 | requires=[
12 | 'tzlocal>=1.5'
13 | ],
14 | zip_safe=False)
15 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | *.py[oc]
2 |
3 | # Temp files
4 | *~
5 | ~*
6 | .*~
7 | \#*
8 | .#*
9 | *#
10 |
11 | # Build files
12 | build
13 | dist
14 | pkg
15 | *.egg
16 | *.egg-info
17 |
18 | # Debian Files
19 | debian/files
20 | debian/python-beaver*
21 |
22 | # Sphinx build
23 | doc/_build
24 |
25 | # Generated man page
26 | doc/aws_hostname.1
27 |
28 | # tox
29 | .tox
30 |
31 | # Hypothesis - keep the examples database
32 | .hypothesis/tmp
33 | .hypothesis/unicodedata
34 |
35 | # py.test
36 | .cache/
37 |
38 | # Pycharm
39 | .idea/
40 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | The MIT License (MIT)
2 |
3 | Copyright (c) 金小龙(jxlwqq)
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/php2python.py:
--------------------------------------------------------------------------------
1 | #! /usr/bin/env python
2 | # -*- coding: utf8 -*-
3 |
4 | from collections import Counter
5 | import random
6 | from functools import reduce
7 | import hashlib
8 | import locale
9 | import csv
10 | import io
11 | import re
12 | import math
13 | import time as py_time
14 | import datetime
15 | import binascii
16 | import urllib.parse
17 | import urllib.request
18 | from itertools import takewhile
19 | import numbers
20 | import pickle
21 | import pprint
22 | import base64
23 | import os
24 | import struct
25 | import sys
26 | import glob as py_glob
27 | import inspect
28 | import textwrap
29 | import crypt as py_crypt
30 | import quopri
31 | import string
32 | import syslog as py_syslog
33 | import fcntl
34 | import fnmatch as py_fnmatch
35 | import shutil
36 | import collections
37 | from socket import inet_ntoa
38 | from socket import inet_aton
39 | import socket
40 | from struct import pack
41 | from struct import unpack
42 | import http.cookies
43 | import codecs
44 |
45 | import tzlocal
46 |
47 | """
48 | Array Functions
49 | """
50 |
51 |
52 | def array_change_key_case(array, case=0):
53 | if case == 0:
54 | f = str.lower
55 | elif case == 1:
56 | f = str.upper
57 | else:
58 | raise ValueError()
59 | return dict((f(k), v) for k, v in array.items())
60 |
61 |
62 | def array_chunk(array, size):
63 | return [array[i: i + size] for i in range(0, len(array), size)]
64 |
65 |
66 | def array_column(array, column_key, index_key=None):
67 | if index_key is None:
68 | return [item.get(column_key) for item in array]
69 | else:
70 | res = {}
71 | for item in array:
72 | res[item.get(index_key)] = item.get(column_key)
73 | return res
74 |
75 |
76 | def array_combine(keys, values):
77 | return dict(zip(keys, values))
78 |
79 |
80 | def array_count_values(array):
81 | return Counter(array)
82 |
83 |
84 | def array_diff_assoc():
85 | pass
86 |
87 |
88 | def array_diff_key():
89 | pass
90 |
91 |
92 | def array_diff_uassoc():
93 | pass
94 |
95 |
96 | def array_diff_ukey():
97 | pass
98 |
99 |
100 | def array_diff(array1, array2):
101 | return list(set(array1).difference(array2))
102 |
103 |
104 | def array_fill_keys(keys, value):
105 | return dict.fromkeys(keys, value)
106 |
107 |
108 | def array_fill(start_index, num, value):
109 | if start_index >= 0:
110 | keys = range(start_index, num + start_index)
111 | else:
112 | keys = [start_index] + list(range(0, num - 1))
113 | return array_fill_keys(keys, value)
114 |
115 |
116 | def array_filter(array, callback=None):
117 | filter(callback, array)
118 |
119 |
120 | def array_flip(array):
121 | return dict((v, k) for k, v in array.items())
122 |
123 |
124 | def array_intersect_assoc(array1, array2):
125 | pass
126 |
127 |
128 | def array_intersect_key(array1, *arrays):
129 | keys = array1.viewkeys()
130 | for array in arrays:
131 | keys &= array.viewkeys()
132 | return {k: array1[k] for k in keys}
133 |
134 |
135 | def array_intersect_uassoc(array1, array2, callback):
136 | pass
137 |
138 |
139 | def array_intersect_ukey(array1, array2, callback):
140 | pass
141 |
142 |
143 | def array_intersect(array1, array2):
144 | pass
145 |
146 |
147 | def array_key_exists(key, array):
148 | return key in array
149 |
150 |
151 | def array_key_first(array):
152 | return array.keys[0]
153 |
154 |
155 | def array_key_last(array):
156 | return array.keys[-1]
157 |
158 |
159 | def array_keys(array, search_value=None):
160 | if search_value is None:
161 | return array.keys()
162 | else:
163 | return [k for k, v in array.items() if v == search_value]
164 |
165 |
166 | def array_map(callback, array):
167 | return map(callback, array)
168 |
169 |
170 | def array_merge_recursive(array1, *arrays):
171 | for array in arrays:
172 | for key, value in array.items():
173 | if key in array1:
174 | if isinstance(value, dict):
175 | array[key] = array_merge_recursive(array1[key], value)
176 | if isinstance(value, (list, tuple)):
177 | array[key] += array1[key]
178 | array1.update(array)
179 | return array1
180 |
181 |
182 | def array_merge(array1, array2):
183 | if isinstance(array1, list) and isinstance(array2, list):
184 | return array1 + array2
185 | elif isinstance(array1, dict) and isinstance(array2, dict):
186 | return dict(list(array1.items()) + list(array2.items()))
187 | elif isinstance(array1, set) and isinstance(array2, set):
188 | return array1.union(array2)
189 | return False
190 |
191 |
192 | def array_multisort(array):
193 | return sorted(array, key=lambda x: (x[1], x[2]))
194 |
195 |
196 | def array_pad(array, size, value):
197 | if size >= 0:
198 | return array + [value] * (size - len(array))
199 | else:
200 | return [value] * (-size - len(array)) + array
201 |
202 |
203 | def array_pop(array):
204 | return array.pop()
205 |
206 |
207 | def array_product(array):
208 | if not array:
209 | return 0
210 | else:
211 | reduce(lambda a, b: a * b, array)
212 |
213 |
214 | def array_push(array, *values):
215 | for value in values:
216 | array.extend(value)
217 |
218 |
219 | def array_rand(array, num=1):
220 | if num == 1:
221 | return random.choice(array.keys())
222 | else:
223 | return random.sample(array.keys(), num)
224 |
225 |
226 | def array_reduce(array, callback, initial=None):
227 | if initial is None:
228 | return reduce(callback, array)
229 | else:
230 | return reduce(function, array, initial)
231 |
232 |
233 | def array_replace_recursive():
234 | pass
235 |
236 |
237 | def array_replace():
238 | pass
239 |
240 |
241 | def array_reverse(array):
242 | return array[::-1]
243 |
244 |
245 | def array_search(needle, haystack, strict=False):
246 | pass
247 |
248 |
249 | def array_shift(array):
250 | return array.pop(0)
251 |
252 |
253 | def array_slice(array, offset, length=None):
254 | if is_array(array) and not isinstance(array, dict):
255 | if isinstance(array, set):
256 | array = list(array)
257 | return set(array[offset:length])
258 | return array[offset:length]
259 | return False
260 |
261 |
262 | def array_splice(array, offset, length, replacement=None):
263 | if replacement is None:
264 | del array[offset: offset + length]
265 | else:
266 | array[offset: offset + length] = replacement
267 | return array
268 |
269 |
270 | def array_sum(array):
271 | return sum(array)
272 |
273 |
274 | def array_udiff_assoc(array):
275 | pass
276 |
277 |
278 | def array_udiff_uassoc(array):
279 | pass
280 |
281 |
282 | def array_udiff(array):
283 | pass
284 |
285 |
286 | def array_uintersect_assoc(array):
287 | pass
288 |
289 |
290 | def array_uintersect_uassoc(array):
291 | pass
292 |
293 |
294 | def array_uintersect(array):
295 | pass
296 |
297 |
298 | def array_unique(array):
299 | return list(set(array))
300 |
301 |
302 | def array_unshift(array, *args):
303 | for i in reversed(args):
304 | array.insert(0, i)
305 | return array
306 |
307 |
308 | def array_values(array):
309 | return array.values()
310 |
311 |
312 | def array_walk_recursive(array):
313 | pass
314 |
315 |
316 | def array_walk(array, callback):
317 | for k, val in array.items():
318 | callback(val, k)
319 |
320 |
321 | def array(array):
322 | pass
323 |
324 |
325 | def arsort(array):
326 | return sorted(array.items(), key=lambda x: x[1], reverse=True)
327 |
328 |
329 | def asort(array):
330 | return sorted(array.items(), key=lambda x: x[1])
331 |
332 |
333 | def compact(*names):
334 | caller = inspect.stack()[1][0]
335 | vars = {}
336 | for n in names:
337 | if n in caller.f_locals:
338 | vars[n] = caller.f_locals[n]
339 | elif n in caller.f_globals:
340 | vars[n] = caller.f_globals[n]
341 | return vars
342 |
343 |
344 | def count(array):
345 | return len(array)
346 |
347 |
348 | def current(array):
349 | pass
350 |
351 |
352 | def each(array):
353 | pass
354 |
355 |
356 | def end(array):
357 | return array[-1]
358 |
359 |
360 | def extract(array):
361 | pass
362 |
363 |
364 | def in_array(needle, haystack):
365 | return needle in haystack
366 |
367 |
368 | def key_exists(key, array):
369 | return array_key_exists(key, array)
370 |
371 |
372 | def key(array):
373 | pass
374 |
375 |
376 | def krsort(array):
377 | return [(k, array[k]) for k in sorted(array.keys(), reverse=True)]
378 |
379 |
380 | def ksort(array):
381 | return [(k, array[k]) for k in sorted(array.keys())]
382 |
383 |
384 | def natcasesort(array):
385 | pass
386 |
387 |
388 | def natsort(array):
389 | pass
390 |
391 |
392 | def pos(array):
393 | pass
394 |
395 |
396 | def prev(array):
397 | pass
398 |
399 |
400 | def reset(array):
401 | pass
402 |
403 |
404 | def rsort(array):
405 | return array.sort(reverse=True)
406 |
407 |
408 | def shuffle(array):
409 | return random.shuffle(array)
410 |
411 |
412 | def sizeof(array):
413 | return len(array)
414 |
415 |
416 | def sort(array):
417 | return array.sort()
418 |
419 |
420 | def uasort(array):
421 | pass
422 |
423 |
424 | def uksort(array):
425 | pass
426 |
427 |
428 | def usort(array):
429 | pass
430 |
431 |
432 | """
433 | Date/Time Functions
434 | """
435 |
436 |
437 | def checkdate(month, day, year):
438 | import datetime
439 | try:
440 | month, day, year = map(int, (month, day, year))
441 | datetime.date(year, month, day)
442 | return True
443 | except ValueError:
444 | return False
445 | pass
446 |
447 |
448 | def date_add():
449 | pass
450 |
451 |
452 | def date_create_from_format():
453 | pass
454 |
455 |
456 | def date_create_immutable_from_format():
457 | pass
458 |
459 |
460 | def date_create_immutable():
461 | pass
462 |
463 |
464 | def date_create():
465 | pass
466 |
467 |
468 | def date_date_set():
469 | pass
470 |
471 |
472 | def date_default_timezone_get():
473 | return tzlocal.get_localzone().zone
474 |
475 |
476 | def date_default_timezone_set(timezone_identifier):
477 | pass
478 |
479 |
480 | def date_diff():
481 | pass
482 |
483 |
484 | def date_format():
485 | pass
486 |
487 |
488 | def date_get_last_errors():
489 | pass
490 |
491 |
492 | def date_interval_create_from_date_string():
493 | pass
494 |
495 |
496 | def date_interval_format():
497 | pass
498 |
499 |
500 | def date_isodate_set():
501 | pass
502 |
503 |
504 | def date_modify():
505 | pass
506 |
507 |
508 | def date_offset_get():
509 | pass
510 |
511 |
512 | def date_parse_from_format():
513 | pass
514 |
515 |
516 | def date_parse():
517 | pass
518 |
519 |
520 | def date_sub():
521 | pass
522 |
523 |
524 | def date_sun_info():
525 | pass
526 |
527 |
528 | def date_sunrise():
529 | pass
530 |
531 |
532 | def date_sunset():
533 | pass
534 |
535 |
536 | def date_time_set():
537 | pass
538 |
539 |
540 | def date_timestamp_get():
541 | pass
542 |
543 |
544 | def date_timestamp_set():
545 | pass
546 |
547 |
548 | def date_timezone_get():
549 | pass
550 |
551 |
552 | def date_timezone_set():
553 | pass
554 |
555 |
556 | def date(format, timestamp=None):
557 | if timestamp is None:
558 | timestamp = py_time.time()
559 | return py_time.strftime(format, timestamp)
560 |
561 |
562 | def getdate(timestamp=None):
563 | if timestamp is None:
564 | timestamp = py_time.time()
565 |
566 |
567 |
568 | def gettimeofday():
569 | pass
570 |
571 |
572 | def gmdate():
573 | pass
574 |
575 |
576 | def gmmktime():
577 | pass
578 |
579 |
580 | def gmstrftime():
581 | pass
582 |
583 |
584 | def idate():
585 | pass
586 |
587 |
588 | def localtime(timestamp):
589 | return py_time.localtime(timestamp)
590 |
591 |
592 | def microtime(get_as_float=False):
593 | d = datetime.now()
594 | t = py_time.mktime(d.timetuple())
595 | if get_as_float:
596 | return t
597 | else:
598 | ms = d.microsecond / 1000000.
599 | return '%f %d' % (ms, t)
600 |
601 |
602 | def mktime(hour, minute, second, month, day, year):
603 | return py_time.mktime((year, month, day, hour, minute, second))
604 |
605 |
606 | def strftime():
607 | pass
608 |
609 |
610 | def strptime():
611 | pass
612 |
613 |
614 | def strtotime():
615 | pass
616 |
617 |
618 | def time():
619 | return int(py_time.time())
620 |
621 |
622 | def timezone_abbreviations_list():
623 | pass
624 |
625 |
626 | def timezone_identifiers_list():
627 | pass
628 |
629 |
630 | def timezone_location_get():
631 | pass
632 |
633 |
634 | def timezone_name_from_abbr():
635 | pass
636 |
637 |
638 | def timezone_name_get():
639 | pass
640 |
641 |
642 | def timezone_offset_get():
643 | pass
644 |
645 |
646 | def timezone_open():
647 | pass
648 |
649 |
650 | def timezone_transitions_get():
651 | pass
652 |
653 |
654 | def timezone_version_get():
655 | pass
656 |
657 |
658 | '''
659 | Filesystem Functions
660 | '''
661 |
662 |
663 | def basename(path, suffix=None):
664 | base_name = os.path.basename(path)
665 | if suffix is not None and basename.endswith(suffix):
666 | return base_name[:-len(suffix)]
667 | return base_name
668 |
669 |
670 | def chgrp(filename, group):
671 | return os.chown(filename, None, group)
672 |
673 |
674 | def chmod(filename, mode):
675 | return os.chmod(filename, mode)
676 |
677 |
678 | def chown(filename, user):
679 | return os.chown(filename, user, None)
680 |
681 |
682 | def clearstatcache():
683 | pass
684 |
685 |
686 | def copy(source, dest):
687 | return shutil.copy(source, dest)
688 |
689 |
690 | def delete(filename):
691 | return unlink(filename)
692 |
693 |
694 | def dirname(path):
695 | return os.path.dirname(path.rstrip(os.pathsep)) or '.'
696 |
697 |
698 | def disk_free_space():
699 | pass
700 |
701 |
702 | def disk_total_space():
703 | pass
704 |
705 |
706 | def diskfreespace():
707 | pass
708 |
709 |
710 | def fclose():
711 | pass
712 |
713 |
714 | def feof():
715 | pass
716 |
717 |
718 | def fflush(handle):
719 | return handle.flush()
720 |
721 |
722 | def fgetc():
723 | pass
724 |
725 |
726 | def fgetcsv():
727 | pass
728 |
729 |
730 | def fgets():
731 | pass
732 |
733 |
734 | def fgetss():
735 | pass
736 |
737 |
738 | def file_exists(path):
739 | return os.path.exists(path)
740 |
741 |
742 | def file_get_contents():
743 | pass
744 |
745 |
746 | def file_put_contents(filename, mode, data):
747 | file = open(filename, mode)
748 | file.write(data)
749 | file.close()
750 |
751 |
752 | def file():
753 | pass
754 |
755 |
756 | def fileatime(filename):
757 | return os.path.getatime(filename)
758 |
759 |
760 | def filectime(filename):
761 | return os.path.getctime(filename)
762 |
763 |
764 | def filegroup(filename):
765 | return os.stat(filename).st_gid
766 |
767 |
768 | def fileinode(filename):
769 | return os.stat(filename).st_ino
770 |
771 |
772 | def filemtime(filename):
773 | return os.path.getmtime(filename)
774 |
775 |
776 | def fileowner(filename):
777 | return os.stat(filename).st_uid
778 |
779 |
780 | def fileperms(filename):
781 | return os.stat(filename).st_mode
782 |
783 |
784 | def filesize(filename):
785 | return os.path.getsize(filename)
786 |
787 |
788 | def filetype():
789 | pass
790 |
791 |
792 | def flock(handle, operation):
793 | return fcntl.flock(handle, operation)
794 |
795 |
796 | def fnmatch(filename, pattern):
797 | return py_fnmatch.fnmatch(filename, pattern)
798 |
799 |
800 | def fopen():
801 | pass
802 |
803 |
804 | def fpassthru():
805 | pass
806 |
807 |
808 | def fputcsv():
809 | pass
810 |
811 |
812 | def fputs():
813 | pass
814 |
815 |
816 | def fread(handle, length):
817 | f = open(handle, "r+")
818 | return f.read(length)
819 |
820 |
821 | def fscanf():
822 | pass
823 |
824 |
825 | def fseek(handle, offset, whence=0):
826 | # SEEK_SET=0
827 | # SEEK_CUR=1
828 | # SEEK_END=2
829 | return handle.seek(offset, whence)
830 |
831 |
832 | def fstat(handle):
833 | os.fstat(handle)
834 |
835 |
836 | def ftell(handle):
837 | return handle.tell()
838 |
839 |
840 | def ftruncate(handle, size):
841 | return handle.truncate(size)
842 |
843 |
844 | def fwrite():
845 | pass
846 |
847 |
848 | def glob(pattern):
849 | return py_glob.glob(pattern)
850 |
851 |
852 | def is_dir(path):
853 | return os.path.isdir(path)
854 |
855 |
856 | def is_executable(filename):
857 | return os.access(filename, os.X_OK)
858 |
859 |
860 | def is_file(filename):
861 | return os.path.isfile(filename)
862 |
863 |
864 | def is_link(filename):
865 | return os.path.islink(filename)
866 |
867 |
868 | def is_readable(filename):
869 | return os.access(filename, os.R_OK)
870 |
871 |
872 | def is_uploaded_file():
873 | pass
874 |
875 |
876 | def is_writable(filename):
877 | return os.access(filename, os.W_OK)
878 |
879 |
880 | def is_writeable(filename):
881 | return os.access(filename, os.W_OK)
882 |
883 |
884 | def lchgrp(filename, group):
885 | os.lchown(filename, None, group)
886 |
887 |
888 | def lchown(filename, user):
889 | return os.lchown(filename, user, None)
890 |
891 |
892 | def link(target, link):
893 | return os.link(target, link)
894 |
895 |
896 | def linkinfo(path):
897 | return os.stat(path).st_dev
898 |
899 |
900 | def lstat(filename):
901 | return os.lstat(filename)
902 |
903 |
904 | def mkdir(pathname, mode):
905 | return os.mkdir(pathname, mode)
906 |
907 |
908 | def move_uploaded_file():
909 | pass
910 |
911 |
912 | def parse_ini_file():
913 | pass
914 |
915 |
916 | def parse_ini_string():
917 | pass
918 |
919 |
920 | def pathinfo():
921 | pass
922 |
923 |
924 | def pclose():
925 | pass
926 |
927 |
928 | def popen():
929 | pass
930 |
931 |
932 | def readfile(filename):
933 | return open(filename, 'r').read()
934 |
935 |
936 | def readlink(path):
937 | return os.readlink(path)
938 |
939 |
940 | def realpath_cache_get():
941 | pass
942 |
943 |
944 | def realpath_cache_size():
945 | pass
946 |
947 |
948 | def realpath(path):
949 | return os.path.realpath(path)
950 |
951 |
952 | def rename(oldname, newname):
953 | return os.rename(oldname, newname)
954 |
955 |
956 | def rewind():
957 | pass
958 |
959 |
960 | def rmdir(dirname):
961 | return os.rmdir(dirname)
962 |
963 |
964 | def set_file_buffer():
965 | pass
966 |
967 |
968 | def stat(filename):
969 | return os.stat(filename)
970 |
971 |
972 | def symlink(target, link):
973 | return os.symlink(target, link)
974 |
975 |
976 | def tempnam():
977 | pass
978 |
979 |
980 | def tmpfile():
981 | pass
982 |
983 |
984 | def touch(filename, atime=None, mtime=None):
985 | return os.utime(filename, (atime, mtime))
986 |
987 |
988 | def umask(mask):
989 | return os.umask(mask)
990 |
991 |
992 | def unlink(filename):
993 | return os.unlink(filename)
994 |
995 |
996 | """
997 | Mathematical Functions
998 | """
999 |
1000 |
1001 | def acos(arg):
1002 | return math.acos(arg)
1003 |
1004 |
1005 | def acosh(arg):
1006 | return math.acosh(arg)
1007 |
1008 |
1009 | def asin(arg):
1010 | return math.asin(arg)
1011 |
1012 |
1013 | def asinh(arg):
1014 | return math.asinh(arg)
1015 |
1016 |
1017 | def atan2(y, x):
1018 | return math.atan2(y, x)
1019 |
1020 |
1021 | def atan(arg):
1022 | return math.atan(arg)
1023 |
1024 |
1025 | def atanh(arg):
1026 | return math.atanh(arg)
1027 |
1028 |
1029 | def base_convert(number, from_base, to_base):
1030 | try:
1031 | base10 = int(number, from_base)
1032 | except ValueError:
1033 | raise
1034 |
1035 | if to_base < 2 or to_base > 36:
1036 | raise NotImplementedError
1037 |
1038 | digits = "0123456789abcdefghijklmnopqrstuvwxyz"
1039 | sign = ''
1040 |
1041 | if base10 == 0:
1042 | return '0'
1043 | elif base10 < 0:
1044 | sign = '-'
1045 | base10 = -base10
1046 |
1047 | s = ''
1048 | while base10 != 0:
1049 | r = base10 % to_base
1050 | r = int(r)
1051 | s = digits[r] + s
1052 | base10 //= to_base
1053 |
1054 | output_value = sign + s
1055 | return output_value
1056 |
1057 |
1058 | def bindec(binary_string):
1059 | return int(binary_string, 2)
1060 |
1061 |
1062 | def ceil(value):
1063 | return math.ceil(value)
1064 |
1065 |
1066 | def cos(arg):
1067 | return math.cos(arg)
1068 |
1069 |
1070 | def cosh(arg):
1071 | return math.cosh(arg)
1072 |
1073 |
1074 | def decbin(number):
1075 | return bin(number)
1076 |
1077 |
1078 | def dechex(number):
1079 | return hex(number)
1080 |
1081 |
1082 | def decoct(number):
1083 | return oct(number)
1084 |
1085 |
1086 | def deg2rad(number):
1087 | return math.radians(number)
1088 |
1089 |
1090 | def exp(arg):
1091 | return math.exp(arg)
1092 |
1093 |
1094 | def expm1(arg):
1095 | return math.exp(arg) - 1
1096 |
1097 |
1098 | def floor(value):
1099 | return math.floor(value)
1100 |
1101 |
1102 | def fmod(x, y):
1103 | return math.fmod(x, y)
1104 |
1105 |
1106 | def getrandmax():
1107 | pass
1108 |
1109 |
1110 | def hexdec(hex_string):
1111 | return int(hex_string, 16)
1112 |
1113 |
1114 | def hypot(x, y):
1115 | return math.hypot(x, y)
1116 |
1117 |
1118 | def intdiv(dividend, divisor):
1119 | pass
1120 |
1121 |
1122 | def is_finite(val):
1123 | return math.isfinite(val)
1124 |
1125 |
1126 | def is_infinite(val):
1127 | return math.isinf(val)
1128 |
1129 |
1130 | def is_nan(val):
1131 | return math.isnan(val)
1132 |
1133 |
1134 | def lcg_value():
1135 | pass
1136 |
1137 |
1138 | def log10(arg):
1139 | return math.log10(arg)
1140 |
1141 |
1142 | def log1p(arg):
1143 | return math.log1p(arg)
1144 |
1145 |
1146 | def log(arg, base):
1147 | return math.log(arg, base)
1148 |
1149 |
1150 | def mt_getrandmax():
1151 | pass
1152 |
1153 |
1154 | def mt_rand(low, high):
1155 | return random.randint(low, high)
1156 |
1157 |
1158 | def mt_srand():
1159 | pass
1160 |
1161 |
1162 | def octdec(octal_string):
1163 | return int(octal_string, 8)
1164 |
1165 |
1166 | def pi():
1167 | return math.pi
1168 |
1169 |
1170 | def rad2deg(number):
1171 | return math.degrees(number)
1172 |
1173 |
1174 | def rand(minint, maxint):
1175 | return random.randint(minint, maxint)
1176 |
1177 |
1178 | def sin(arg):
1179 | return math.sin(arg)
1180 |
1181 |
1182 | def sinh(arg):
1183 | return math.sinh(arg)
1184 |
1185 |
1186 | def sqrt(arg):
1187 | return math.sqrt(arg)
1188 |
1189 |
1190 | def srand(seed=None):
1191 | if seed is None:
1192 | return random.seed()
1193 | return random.seed(seed)
1194 |
1195 |
1196 | def tan(arg):
1197 | return math.tan(arg)
1198 |
1199 |
1200 | def tanh(arg):
1201 | return math.tanh(arg)
1202 |
1203 |
1204 | '''
1205 | Misc. Functions
1206 | '''
1207 |
1208 |
1209 | def connection_aborted():
1210 | pass
1211 |
1212 |
1213 | def connection_status():
1214 | pass
1215 |
1216 |
1217 | def constant():
1218 | pass
1219 |
1220 |
1221 | def define():
1222 | pass
1223 |
1224 |
1225 | def defined():
1226 | pass
1227 |
1228 |
1229 | def die():
1230 | sys.exit()
1231 |
1232 |
1233 | def get_browser():
1234 | pass
1235 |
1236 |
1237 | def __halt_compiler():
1238 | pass
1239 |
1240 |
1241 | def highlight_file():
1242 | pass
1243 |
1244 |
1245 | def highlight_string():
1246 | pass
1247 |
1248 |
1249 | def hrtime():
1250 | pass
1251 |
1252 |
1253 | def ignore_user_abort():
1254 | pass
1255 |
1256 |
1257 | def pack(format_codes, args):
1258 | return struct.pack(format_codes, args)
1259 |
1260 |
1261 | def php_check_syntax():
1262 | pass
1263 |
1264 |
1265 | def php_strip_whitespace():
1266 | pass
1267 |
1268 |
1269 | def sapi_windows_cp_conv():
1270 | pass
1271 |
1272 |
1273 | def sapi_windows_cp_get():
1274 | pass
1275 |
1276 |
1277 | def sapi_windows_cp_is_utf8():
1278 | pass
1279 |
1280 |
1281 | def sapi_windows_cp_set():
1282 | pass
1283 |
1284 |
1285 | def sapi_windows_vt100_support():
1286 | pass
1287 |
1288 |
1289 | def show_source():
1290 | pass
1291 |
1292 |
1293 | def sleep(seconds):
1294 | py_time.sleep(seconds)
1295 |
1296 |
1297 | def sys_getloadavg():
1298 | return os.getloadavg()
1299 |
1300 |
1301 | def time_nanosleep(seconds, nanoseconds):
1302 | pass
1303 |
1304 |
1305 | def time_sleep_until(timestamp):
1306 | py_time.sleep(timestamp - py_time.time())
1307 |
1308 |
1309 | def uniqid(prefix=''):
1310 | return prefix + hex(int(py_time.time()))[2:10] + hex(int(py_time.time() * 1000000) % 0x100000)[2:7]
1311 |
1312 |
1313 | def unpack(format_codes, data):
1314 | return struct.unpack(format_codes, data)
1315 |
1316 |
1317 | def usleep(micro_seconds):
1318 | py_time.sleep(micro_seconds / 1000000.0)
1319 |
1320 |
1321 | '''
1322 | Network Functions
1323 | '''
1324 |
1325 |
1326 | def checkdnsrr():
1327 | pass
1328 |
1329 |
1330 | def closelog():
1331 | return py_syslog.closelog()
1332 |
1333 |
1334 | def define_syslog_variables():
1335 | pass
1336 |
1337 |
1338 | def dns_check_record():
1339 | pass
1340 |
1341 |
1342 | def dns_get_mx(hostname):
1343 | pass
1344 |
1345 |
1346 | def dns_get_record():
1347 | pass
1348 |
1349 |
1350 | def fsockopen(hostname, port):
1351 | sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
1352 | sock.connect((hostname, port))
1353 | return sock.makefile()
1354 |
1355 |
1356 | def gethostbyaddr(ip_address):
1357 | return socket.gethostbyaddr(ip_address)
1358 |
1359 |
1360 | def gethostbyname(hostname):
1361 | return socket.gethostbyname(hostname)
1362 |
1363 |
1364 | def gethostbynamel(hostname):
1365 | return socket.gethostbyname(hostname)
1366 |
1367 |
1368 | def gethostname():
1369 | return socket.gethostname()
1370 |
1371 |
1372 | def getmxrr():
1373 | pass
1374 |
1375 |
1376 | def getprotobyname(name):
1377 | return socket.getprotobyname(name)
1378 |
1379 |
1380 | def getprotobynumber(number):
1381 | table = {num: name[8:] for name, num in vars(socket).items() if name.startswith("IPPROTO")}
1382 | return table[number]
1383 |
1384 |
1385 | def getservbyname(service, protocol):
1386 | return socket.getservbyname(service, protocol)
1387 |
1388 |
1389 | def getservbyport(port, protocol):
1390 | return socket.getservbyport(port, protocol)
1391 |
1392 |
1393 | def header_register_callback():
1394 | pass
1395 |
1396 |
1397 | def header_remove():
1398 | pass
1399 |
1400 |
1401 | def header():
1402 | pass
1403 |
1404 |
1405 | def headers_list():
1406 | pass
1407 |
1408 |
1409 | def headers_sent():
1410 | pass
1411 |
1412 |
1413 | def http_response_code():
1414 | pass
1415 |
1416 |
1417 | def inet_ntop(in_addr):
1418 | return socket.inet_ntop(socket.AF_INET, in_addr)
1419 |
1420 |
1421 | def inet_pton(address):
1422 | return socket.inet_pton(socket.AF_INET, address)
1423 |
1424 |
1425 | def ip2long(ip_addr):
1426 | return unpack("!L", inet_aton(ip_addr))[0]
1427 |
1428 |
1429 | def long2ip(ip):
1430 | return inet_ntoa(pack("!L", ip))
1431 |
1432 |
1433 | def openlog(ident, option, facility):
1434 | return py_syslog.openlog(ident, option, facility)
1435 |
1436 |
1437 | def pfsockopen():
1438 | pass
1439 |
1440 |
1441 | def setcookie(name, value='', expire=0, path='', domain=''):
1442 | cookie = http.cookies.SimpleCookie()
1443 | cookie[name] = value
1444 | cookie[name]['domain'] = domain
1445 | cookie[name]['path'] = path
1446 | cookie[name]['expires'] = expire if expire != 0 else py_time.strftime("%a, %d-%b-%Y %H:%M:%S GMT")
1447 | return cookie.output()
1448 |
1449 |
1450 | def setrawcookie():
1451 | pass
1452 |
1453 |
1454 | def socket_get_status():
1455 | pass
1456 |
1457 |
1458 | def socket_set_blocking():
1459 | pass
1460 |
1461 |
1462 | def socket_set_timeout():
1463 | pass
1464 |
1465 |
1466 | def syslog(priority, message):
1467 | return py_syslog.syslog(priority, message)
1468 |
1469 |
1470 | '''
1471 | Program execution Functions
1472 | '''
1473 |
1474 |
1475 | def escapeshellarg(arg):
1476 | return "\\'".join("'" + p + "'" for p in arg.split("'"))
1477 |
1478 |
1479 | def escapeshellcmd():
1480 | pass
1481 |
1482 |
1483 | def passthru():
1484 | pass
1485 |
1486 |
1487 | def proc_lose():
1488 | pass
1489 |
1490 |
1491 | def proc_et_tatus():
1492 | pass
1493 |
1494 |
1495 | def proc_ice():
1496 | pass
1497 |
1498 |
1499 | def proc_pen():
1500 | pass
1501 |
1502 |
1503 | def proc_erminate():
1504 | pass
1505 |
1506 |
1507 | def shell_xec(command):
1508 | return os.popen(command).read()
1509 |
1510 |
1511 | def system(command):
1512 | return os.system(command)
1513 |
1514 |
1515 | """
1516 | Strings Functions
1517 | """
1518 |
1519 |
1520 | def addcslashes(string):
1521 | pass
1522 |
1523 |
1524 | def addslashes(string):
1525 | pass
1526 |
1527 |
1528 | def bin2hex(string):
1529 | return binascii.hexlify(string)
1530 |
1531 |
1532 | def chop(string, character_mask=None):
1533 | return rtrim(string, character_mask)
1534 |
1535 |
1536 | def chunk_split(body, chunklen, end="\r\n"):
1537 | return end.join(textwrap.wrap(body, chunklen))
1538 |
1539 |
1540 | def convert_cyr_string(string):
1541 | pass
1542 |
1543 |
1544 | def convert_uudecode(string):
1545 | pass
1546 |
1547 |
1548 | def convert_uuencode(string):
1549 | pass
1550 |
1551 |
1552 | def count_chars(s, mode=0):
1553 | temp = {chr(_x): 0 for _x in range(256)}
1554 | if mode == 0:
1555 | temp.update(Counter(s))
1556 | return temp
1557 | elif mode == 1:
1558 | temp.update(Counter(s))
1559 | res = temp.copy()
1560 | for i, j in temp.items():
1561 | if not j:
1562 | res.pop(i)
1563 | return res
1564 | elif mode == 2:
1565 | temp.update(Counter(s))
1566 | res = temp.copy()
1567 | for i, j in temp.items():
1568 | if j:
1569 | res.pop(i)
1570 | return res
1571 | elif mode == 3:
1572 | res = ""
1573 | temp.update(Counter(s))
1574 | for i, j in temp.items():
1575 | if j:
1576 | res += i
1577 | return res
1578 | elif mode == 4:
1579 | res = ""
1580 | temp.update(Counter(s))
1581 | for i, j in temp.items():
1582 | if not j:
1583 | res += i
1584 | return res
1585 | else:
1586 | raise ValueError("Incorrect value of mode (%d)" % (mode,))
1587 |
1588 |
1589 | def crc32(string):
1590 | return binascii.crc32(string) & 0xffffffff
1591 |
1592 |
1593 | def crypt(string, salt):
1594 | return py_crypt.crypt(string, salt)
1595 |
1596 |
1597 | def echo(string):
1598 | print(string)
1599 |
1600 |
1601 | def explode(delimiter, string, limit):
1602 | if limit == 0:
1603 | limit = 1
1604 |
1605 | if limit > 0:
1606 | return string.split(delimiter, limit)
1607 | else:
1608 | return string.split(delimiter)[:limit]
1609 |
1610 |
1611 | def fprintf(handle, format):
1612 | pass
1613 |
1614 |
1615 | def get_html_translation_table(string):
1616 | pass
1617 |
1618 |
1619 | def hebrev(string):
1620 | pass
1621 |
1622 |
1623 | def hebrevc(string):
1624 | pass
1625 |
1626 |
1627 | def hex2bin(hex_string):
1628 | return binascii.unhexlify(hex_string)
1629 |
1630 |
1631 | def html_entity_decode(string):
1632 | pass
1633 |
1634 |
1635 | def htmlentities(string):
1636 | pass
1637 |
1638 |
1639 | def htmlspecialchars_decode(string):
1640 | pass
1641 |
1642 |
1643 | def htmlspecialchars(string):
1644 | pass
1645 |
1646 |
1647 | def implode(glue='', pieces=[]):
1648 | return glue.join(pieces)
1649 |
1650 |
1651 | def join(glue='', pieces=[]):
1652 | return glue.join(pieces)
1653 |
1654 |
1655 | def lcfirst(string):
1656 | return string[0].lower() + string[1:]
1657 |
1658 |
1659 | def levenshtein(string1, string2):
1660 | n, m = len(string1), len(string2)
1661 | if n > m:
1662 | string1, string2 = string2, string1
1663 | n, m = m, n
1664 |
1665 | current = range(n + 1)
1666 | for i in range(1, m + 1):
1667 | previous, current = current, [i] + [0] * n
1668 | for j in range(1, n + 1):
1669 | add, delete = previous[j] + 1, current[j - 1] + 1
1670 | change = previous[j - 1]
1671 | if string1[j - 1] != string2[i - 1]:
1672 | change = change + 1
1673 | current[j] = min(add, delete, change)
1674 |
1675 | return current[n]
1676 |
1677 |
1678 | def localeconv(string):
1679 | pass
1680 |
1681 |
1682 | def ltrim(string, character_mask=None):
1683 | if character_mask is None:
1684 | return string.lstrip()
1685 | return string.lstrip(character_mask)
1686 |
1687 |
1688 | def md5_file(filename, raw_output=False):
1689 | crc = hashlib.md5()
1690 | fp = open(filename, 'rb')
1691 | for i in fp:
1692 | crc.update(i)
1693 | fp.close()
1694 | if raw_output:
1695 | return crc.digest()
1696 | return crc.hexdigest()
1697 |
1698 |
1699 | def md5(str, raw_output=False):
1700 | res = hashlib.md5(str.encode())
1701 | if raw_output:
1702 | return res.digest()
1703 | return res.hexdigest()
1704 |
1705 |
1706 | def metaphone(string):
1707 | pass
1708 |
1709 |
1710 | def money_format(string):
1711 | pass
1712 |
1713 |
1714 | def nl_langinfo(string):
1715 | pass
1716 |
1717 |
1718 | def nl2br(string, is_xhtml=True):
1719 | if is_xhtml:
1720 | return string.replace('\n', '
\n')
1721 | else:
1722 | return string.replace('\n', '
\n')
1723 |
1724 |
1725 | def number_format(number, decimals):
1726 | locale.setlocale(locale.LC_NUMERIC, '')
1727 | return locale.format("%.*f", (decimals, number), True)
1728 |
1729 |
1730 | def parse_str(string):
1731 | return urllib.parse.parse_qs(string)
1732 |
1733 |
1734 | def printf(string):
1735 | return print(string)
1736 |
1737 |
1738 | def quoted_printable_decode(string):
1739 | return quopri.decodestring(string)
1740 |
1741 |
1742 | def quoted_printable_encode(string):
1743 | return quopri.encodestring(string)
1744 |
1745 |
1746 | def quotemeta(string):
1747 | pass
1748 |
1749 |
1750 | def rtrim(string, character_mask=None):
1751 | if character_mask is None:
1752 | return string.rstrip()
1753 | return string.rstrip(character_mask)
1754 |
1755 |
1756 | def setlocale(string):
1757 | pass
1758 |
1759 |
1760 | def sha1_file(filename, raw_output=False):
1761 | crc = hashlib.sha1()
1762 | fp = open(filename, 'rb')
1763 | for i in fp:
1764 | crc.update(i)
1765 | fp.close()
1766 | if raw_output:
1767 | return crc.digest()
1768 | return crc.hexdigest()
1769 |
1770 |
1771 | def sha1(string):
1772 | return hashlib.sha1(string.encode()).hexdigest()
1773 |
1774 |
1775 | def similar_text(string):
1776 | pass
1777 |
1778 |
1779 | def soundex(string):
1780 | pass
1781 |
1782 |
1783 | def sprintf(string):
1784 | pass
1785 |
1786 |
1787 | def sscanf(string):
1788 | pass
1789 |
1790 |
1791 | def str_getcsv(string, delimiter=',', enclosure='"', escape="\\"):
1792 | with io.StringIO(string) as f:
1793 | reader = csv.reader(f, delimiter=delimiter, quotechar=enclosure, escapechar=escape)
1794 | return next(reader)
1795 |
1796 |
1797 | def str_ireplace(search, replace, subject, count=0):
1798 | pattern = re.compile(search, re.IGNORECASE)
1799 | return pattern.sub(replace, subject, count)
1800 |
1801 |
1802 | def str_pad(string, pad_length, pad_string=' ', pad_type=1):
1803 | # STR_PAD_LEFT = 0
1804 | # STR_PAD_RIGHT = 1
1805 | # STR_PAD_BOTH = 2
1806 | if pad_type == 0:
1807 | return string.ljust(pad_length, pad_string)
1808 | elif pad_type == 2:
1809 | return string.center(pad_length, pad_string)
1810 | else:
1811 | return string.rjust(pad_length, pad_string)
1812 |
1813 |
1814 | def str_repeat(string, multiplier):
1815 | return string * multiplier
1816 |
1817 |
1818 | def str_replace(search, replace, subject, count=-1):
1819 | return subject.replace(search, replace, count)
1820 |
1821 |
1822 | def str_rot13(string):
1823 | enc = codecs.getencoder("rot-13")
1824 | return enc(string)[0]
1825 |
1826 |
1827 | def str_shuffle(string):
1828 | chars = list(string)
1829 | random.shuffle(chars)
1830 | return ''.join(chars)
1831 |
1832 |
1833 | def str_split(string, split_length=1):
1834 | return filter(None, re.split('(.{1,%d})' % split_length, string))
1835 |
1836 |
1837 | def str_word_count(string, format=0, charlist=''):
1838 | if isinstance(string, str):
1839 | words = re.sub('[^\w ' + charlist + ']', '', string)
1840 | words = words.replace(' ', ' ').split(' ')
1841 | if format == 0:
1842 | return len(words)
1843 | elif format == 1:
1844 | return words
1845 | elif format == 2:
1846 | result = {}
1847 | for word in words:
1848 | result[string.find(word)] = word
1849 | return result
1850 | return False
1851 |
1852 |
1853 | def strcasecmp(string):
1854 | pass
1855 |
1856 |
1857 | def strchr(haystack, needle):
1858 | pos = haystack.find(needle)
1859 | if pos < 0:
1860 | return None
1861 | else:
1862 | return haystack[pos:]
1863 |
1864 |
1865 | def strcmp(string1, string2):
1866 | return (string1 > string2) - (string1 < string2)
1867 |
1868 |
1869 | def strcoll(string):
1870 | pass
1871 |
1872 |
1873 | def strcspn(string1, string2):
1874 | return len(list(takewhile(lambda x: x not in string2, string1)))
1875 |
1876 |
1877 | def strip_tags(string):
1878 | pass
1879 |
1880 |
1881 | def stripcslashes(string):
1882 | pass
1883 |
1884 |
1885 | def stripos(haystack, needle, offset=0):
1886 | return haystack.upper().find(needle.upper(), offset)
1887 |
1888 |
1889 | def stripslashes(string):
1890 | pass
1891 |
1892 |
1893 | def stristr(haystack, needle):
1894 | pos = haystack.upper().find(needle.upper())
1895 | if pos < 0:
1896 | return None
1897 | else:
1898 | return haystack[pos:]
1899 |
1900 |
1901 | def strlen(string):
1902 | return len(string)
1903 |
1904 |
1905 | def strnatcasecmp(string):
1906 | pass
1907 |
1908 |
1909 | def strnatcmp(string):
1910 | pass
1911 |
1912 |
1913 | def strncasecmp(string):
1914 | pass
1915 |
1916 |
1917 | def strncmp(string):
1918 | pass
1919 |
1920 |
1921 | def strpbrk(haystack, char_list):
1922 | try:
1923 | pos = next(i for i, x in enumerate(haystack) if x in char_list)
1924 | return haystack[pos:]
1925 | except:
1926 | return None
1927 |
1928 |
1929 | def strpos(haystack, needle, offset=0):
1930 | pos = haystack.find(needle, offset)
1931 | if pos == -1:
1932 | return False
1933 | else:
1934 | return pos
1935 |
1936 |
1937 | def strrchr(haystack, needle):
1938 | return haystack.rfind(needle)
1939 |
1940 |
1941 | def strrev(string):
1942 | return string[::-1]
1943 |
1944 |
1945 | def strripos(haystack, needle, offset=0):
1946 | return haystack.upper().rfind(needle.upper(), offset)
1947 |
1948 |
1949 | def strrpos(haystack, needle, offset=0):
1950 | pos = haystack.rfind(needle, offset)
1951 | if pos == -1:
1952 | return False
1953 | else:
1954 | return pos
1955 |
1956 |
1957 | def strspn(subject, mask, start=0, length=None):
1958 | if not length: length = len(subject)
1959 | return len(re.search('^[' + mask + ']*', subject[start:start + length]).group(0))
1960 |
1961 |
1962 | def strstr(haystack, needle):
1963 | pos = haystack.find(needle)
1964 | if pos < 0:
1965 | return None
1966 | else:
1967 | return haystack[pos:]
1968 |
1969 |
1970 | def strtok(string):
1971 | pass
1972 |
1973 |
1974 | def strtolower(string):
1975 | return string.lower()
1976 |
1977 |
1978 | def strtoupper(string):
1979 | return string.upper()
1980 |
1981 |
1982 | def strtr(string, from_str, to_str=None):
1983 | if is_array(from_str):
1984 | return string.translate(str.maketrans(from_str))
1985 | return string.translate(str.maketrans(from_str, to_str))
1986 |
1987 |
1988 | def substr_compare(string):
1989 | pass
1990 |
1991 |
1992 | def substr_count(haystack, needle, offset=0, length=0):
1993 | if offset == 0:
1994 | return haystack.count(needle)
1995 | else:
1996 | if length == 0:
1997 | return haystack.count(needle, offset)
1998 | else:
1999 | return haystack.count(needle, offset, offset + length)
2000 |
2001 |
2002 | def substr_replace(subject, replace, start, length=None):
2003 | if length is None:
2004 | return subject[:start] + replace
2005 | elif length < 0:
2006 | return subject[:start] + replace + subject[length:]
2007 | else:
2008 | return subject[:start] + replace + subject[start + length:]
2009 |
2010 |
2011 | def substr(string, start, length=None):
2012 | if len(string) >= start:
2013 | if start > 0:
2014 | return False
2015 | else:
2016 | return string[start:]
2017 | if not length:
2018 | return string[start:]
2019 | elif length > 0:
2020 | return string[start:start + length]
2021 | else:
2022 | return string[start:length]
2023 |
2024 |
2025 | def trim(string, character_mask=None):
2026 | if character_mask is None:
2027 | return string.strip()
2028 | return string.strip(character_mask)
2029 |
2030 |
2031 | def ucfirst(string):
2032 | return string[0].upper() + string[1:]
2033 |
2034 |
2035 | def ucwords(words):
2036 | return string.capwords(words)
2037 |
2038 |
2039 | def vfprintf(string):
2040 | pass
2041 |
2042 |
2043 | def vprintf(string):
2044 | pass
2045 |
2046 |
2047 | def vsprintf(string):
2048 | pass
2049 |
2050 |
2051 | def wordwrap(string, width):
2052 | return textwrap.wrap(string, width)
2053 |
2054 |
2055 | '''
2056 | URL Functions
2057 | '''
2058 |
2059 |
2060 | def base64_decode(data):
2061 | return base64.b64decode(data)
2062 |
2063 |
2064 | def base64_encode(data):
2065 | return base64.encode(data)
2066 |
2067 |
2068 | def get_headers(url):
2069 | return urllib.request.urlopen('%s' % url).headers
2070 |
2071 |
2072 | def get_meta_tags(url):
2073 | out = {}
2074 | html = urllib.request.urlopen('%s' % url).read()
2075 | m = re.findall("name=\"([^\"]*)\" content=\"([^\"]*)\"", html)
2076 | for i in m:
2077 | out[i[0]] = i[1]
2078 | return out
2079 |
2080 |
2081 | def http_build_query(query_data):
2082 | return urllib.parse.urlencode(query_data)
2083 |
2084 |
2085 | def parse_url(url):
2086 | return urllib.parse.urlparse(url)
2087 |
2088 |
2089 | def rawurldecode(string):
2090 | return urllib.parse.unquote(string)
2091 |
2092 |
2093 | def rawurlencode(string):
2094 | return urllib.parse.quote(string)
2095 |
2096 |
2097 | def urldecode(string):
2098 | return urllib.parse.unquote_plus(string)
2099 |
2100 |
2101 | def urlencode(string):
2102 | return urllib.parse.quote_plus(string)
2103 |
2104 |
2105 | '''
2106 | Variable handling Functions
2107 | '''
2108 |
2109 |
2110 | def boolval(variable):
2111 | return bool(variable)
2112 |
2113 |
2114 | def debug_zval_dump():
2115 | pass
2116 |
2117 |
2118 | def doubleval(variable):
2119 | return float(variable)
2120 |
2121 |
2122 | def empty(variable):
2123 | if not variable:
2124 | return True
2125 | return False
2126 |
2127 |
2128 | def floatval(variable):
2129 | return float(variable)
2130 |
2131 |
2132 | def get_defined_vars():
2133 | pass
2134 |
2135 |
2136 | def get_resource_type():
2137 | pass
2138 |
2139 |
2140 | def gettype(variable):
2141 | return type(variable).__name__
2142 |
2143 |
2144 | def import_request_variables():
2145 | pass
2146 |
2147 |
2148 | def intval(variable, base=10):
2149 | return int(variable, base)
2150 |
2151 |
2152 | def is_array(variable):
2153 | return isinstance(variable, (list, tuple))
2154 |
2155 |
2156 | def is_bool(variable):
2157 | return isinstance(variable, bool)
2158 |
2159 |
2160 | def is_callable(name):
2161 | return callable(name)
2162 |
2163 |
2164 | def is_countable(variable):
2165 | try:
2166 | Counter(variable)
2167 | return True
2168 | except:
2169 | return False
2170 |
2171 |
2172 | def is_double(variable):
2173 | return isinstance(variable, float)
2174 |
2175 |
2176 | def is_float(variable):
2177 | return isinstance(variable, float)
2178 |
2179 |
2180 | def is_int(variable):
2181 | return isinstance(variable, int)
2182 |
2183 |
2184 | def is_integer(variable):
2185 | return isinstance(variable, int)
2186 |
2187 |
2188 | def is_iterable(variable):
2189 | return is_array(variable) or isinstance(variable, collections.Iterable)
2190 |
2191 |
2192 | def is_long(variable):
2193 | return isinstance(variable, int)
2194 |
2195 |
2196 | def is_null(variable):
2197 | return variable is None
2198 |
2199 |
2200 | def is_numeric(variable):
2201 | return isinstance(variable, numbers.Number) or variable.isnumeric()
2202 |
2203 |
2204 | def is_object(variable):
2205 | return isinstance(variable, object)
2206 |
2207 |
2208 | def is_real(variable):
2209 | return isinstance(variable, float)
2210 |
2211 |
2212 | def is_resource():
2213 | pass
2214 |
2215 |
2216 | def is_scalar(variable):
2217 | return isinstance(variable, (type(None), str, int, float, bool))
2218 |
2219 |
2220 | def is_string(variable):
2221 | return isinstance(variable, str)
2222 |
2223 |
2224 | def isset(variable):
2225 | try:
2226 | variable
2227 | return True
2228 | except NameError:
2229 | return False
2230 |
2231 |
2232 | def print_r(variable):
2233 | pprint.pprint(variable)
2234 |
2235 |
2236 | def serialize(value):
2237 | return pickle.dump(value)
2238 |
2239 |
2240 | def settype(variable, variable_type):
2241 | pass
2242 |
2243 |
2244 | def strval(variable):
2245 | return str(variable)
2246 |
2247 |
2248 | def unserialize():
2249 | pass
2250 |
2251 |
2252 | def unset(variable):
2253 | del variable
2254 |
2255 |
2256 | def var_dump(variable):
2257 | print(variable)
2258 |
2259 |
2260 | def var_export(variable):
2261 | print(variable)
2262 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # PHP2Python
2 |
3 | Python alternatives for PHP internal (built-in) functions. Support Python 3 Only.
4 |
5 | * [php2go](https://github.com/syyongx/php2go) - Use Golang to implement PHP's common built-in functions
6 |
7 | ## Table of Contents
8 | * [Array Functions](#array-functions)
9 | * [Date/Time Functions](#datetime-functions)
10 | * [Filesystem Functions](#filesystem-functions)
11 | * [Mathematical Functions](#mathematical-functions)
12 | * [Misc. Functions](#misc-functions)
13 | * [Network Functions](#network-functions)
14 | * [Program execution Functions](#program-execution-functions)
15 | * [String Functions](#string-functions)
16 | * [URL Functions](#url-functions)
17 | * [Variable handling Functions](#variable-handling-functions)
18 |
19 | ## Array Functions
20 |
21 |