-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpy2cheat.py
More file actions
executable file
·579 lines (481 loc) · 19 KB
/
Copy pathpy2cheat.py
File metadata and controls
executable file
·579 lines (481 loc) · 19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
How to use:
1. Open python shell in the directory where this file resides
2. import pycheat
3. pycheat.test_function()
After modification of this file, you should reload the module:
reload(pycheat)
If you want to call this file directly, try to execute from shell: pycheat.py -h
"""
# Module constant - https://www.python.org/dev/peps/pep-0008/#constants
MY_MODULE_CONSTANT = "Module constant"
# Exceptions
class MyException(Exception):
'''
All exceptions are subclasses of BaseException.
Exception is also subclass of BaseException and is the parent class
for all non-exit exceptions.
'''
pass
def exceptions():
try:
raise MyException('Some description')
except MyException as e:
print('The error occurred: {}').format(e)
# Enum class
# usage: print Colours.Red
class Colours:
Red, Green, Blue, White = range(4)
class MyClass:
def __init__(self, attr):
self.my_attr = attr
def date_magic(self):
import datetime
one_day_maybe = datetime.datetime.strptime("2018-09-21 21:49:55", "%Y-%m-%d %H:%M:%S")
today = datetime.datetime.now()
def docstrings(param1, param2):
"""This is first line - doc string should start here.
Docstrings should be enclosed by three double quotes.
Docstrings are used also for introspection.
Example usage of reST (=reStructuredText) documenting variables:
:param timestamp: formatted date to display
:type timestamp: str or unicode
:returns: formatted string
:rtype: str or unicode
Last line of docstring should contain just three double quotes.
"""
return "example string"
def datatypes():
import numbers
# is the variable a number?
my_variable = 3.14
isinstance(my_variable, numbers.Number)
# determine iterables - str, dict, list...
my_var = [1, 2, 3]
import collections
print "Is my_var iterable? - %s" % isinstance(my_var, collections.Iterable)
# strings
# interpolation can be made with both single or double quotes, but usually used with double
single_quote_str = 'single quotes for short strings typically not intended for humans'
double_quote_str = "double quotes for longer strings typically intended for humans"
# raw string literal - the string is used directly without escaping and interpolations
raw_str = r'this is raw string - all special characters has no meaning - {} abcd \n\n etc.'
# unicode string
unicode_str = u'my unicode string ěščřžáíé'
def lists():
l = [1, 2, 'three'] # old-style: l = list(1, 2, 'three')
# filter the list
int_list = [1, 2, 3, 4]
print "Even numbers: %s" % filter(lambda i: i % 2 == 0, int_list)
print "Even numbers: %s" % [i for i in int_list if i % 2 == 0]
def dictionaries():
# create
number_one_players = {'man': 'Andy Murray', 'woman': 'Angelique Kerber'}
# merging dicts
en = {1: 'one', 2: 'two'}
cs = {2: 'dve', 3: 'tri'} # overwrites the key 2
# Python 2
en.update(cs) # {1: 'one', 2: 'dve', 3: 'tri'}
# Python 3
#en = {**en, **cs}
# zip two lists into the dict
keys = ['a', 'b', 'c']
values = [1, 2, 3]
dictionary = dict(zip(keys, values))
print dictionary #{'a': 1, 'b': 2, 'c': 3}
# ordered dicts
from collections import OrderedDict
orig = {
'roger': {'gs_titles': 17},
'rafa': {'gs_titles': 14},
'nole': {'gs_titles': 12},
'andy': {'gs_titles': 3}
}
# dictionary sorted by key
orddict1 = OrderedDict(sorted(orig.items(), key=lambda t: t[0]))
print orddict1 # {andy: ..., nole: ..., rafa: ..., roger: ...}
# dictionary sorted by GS titles
orddict2 = OrderedDict(sorted(orig.items(), key=lambda t: t[1]['gs_titles']))
print orddict2 # {roger: ..., rafa: ..., nole: ..., andy: ...}
def classes():
# abstract methods
class Base(object):
def go(self):
raise NotImplementedError("Method is not implemented.")
def iterate_object():
"""
Examples: https://stackoverflow.com/questions/4019971/how-to-implement-iter-self-for-a-container-object-python
"""
# Simplest case - use generator function (with yield)
class IterClass1(object):
def __iter__(self):
yield 5
for x in range(3):
yield x
iter_class_object = IterClass1()
# will print the sequence: 5, 0, 1, 2
for val in iter_class_object:
print val
# Inherit from appropriate collection
from collections import Sequence
class MyContainer(Sequence):
def __init__(self, *data):
self.data = list(data)
def __getitem__(self, index):
return self.data[index]
def __len__(self):
return len(self.data)
cont = MyContainer(1, "two", 3, 4.0)
print "\nContainer 2:"
for val in cont:
print val
# If container is its own iterator, just implement next method
from collections import Iterator
class IterClass2(Iterator):
def __init__(self, data):
self.data = data
def next(self):
if not self.data:
raise StopIteration
return self.data.pop()
# alternative when we do not want to use base Iterator class:
#def __iter__(self):
# return self
iterable_object = IterClass2(["a", 2, 3])
print "\nContainer 3:"
for val in iterable_object:
print val
def printing():
value1 = "val1"
value2 = "val2"
titles = {'roger': 20, 'rafa': 16}
# python3 style
print('Just print format args positionally: {}, {}'.format(1, 2))
print("Printing args from defined position {1} and {0}".format(value1, value2))
print("Roger has {roger} GS titles and Rafa {rafa}".format(roger=20, rafa=16))
print("Roger has {roger} GS titles and Rafa {rafa}".format(**titles))
print('Escaping the curly braces by doubling them - {{}} and normal arg: {}'.format('myarg'))
# printint to stderr
# python 2
import sys
print >> sys.stderr, "This is the testing error, don't panic!"
# for python 3:
#from __future__ import print_funtion
#print("This is the testing error, don't panic!, file=sys.stderr)
# rounding floats
print("%.1f" % 3.141)
# round float to decimal
print('Rounded to decimal: {.0f}'.format(3.14))
# print with leading zeros
for i in range(5):
print "%02d" % i
# print dict (named args)
print "User %(username)s is from %(city)s. I love %(city)s city!" % {'username': "niwics", 'city': "Brno"}
# Pretty-printing
hierarchical_dict = {'a': 1, 'b': [{'ba': 2, 'bb': 3}]}
import pprint
pp = pprint.PrettyPrinter(indent=5)
pp.pprint(hierarchical_dict)
def logging():
v1 = "variable for testing"
v2 = 123
import logging
# logger names can be hierarchically set
log = logging.getLogger("app_name." + __name__)
log.setLevel("INFO")
# create console handler
ch = logging.StreamHandler()
# create formatter and add it to the handlers
# display just seconds: add second parameter to Formatter: "%Y-%m-%d %H:%M:%S"
formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s {%(name)s: %(lineno)s}')
ch.setFormatter(formatter)
# add the handlers to the logger
log.addHandler(ch)
log.debug("Debug message with variables %s - %d", v1, v2)
log.info("Info message with variables %s - %d", v1, v2)
log.warning("Warning message with variables %s - %d", v1, v2)
log.error("Error message with variables %s - %d", v1, v2)
def regexps():
# Regexps
# https://docs.python.org/2/library/re.html
# important thig when working with regexp patterns: do not forget to use
# raw strings = r"xyz"
import re
# basic searching
match = re.search(r"ne+dle", "searching neeedle in the haystack!")
print match.group(0) # neeedle
# precompiling + group matching
regexp = re.compile(r"(\d+):\d+")
match = regexp.search("arrival in 12:00")
print "arrival hour: %s" % match.group(1)
# simple replacing
print re.sub(r"@", r"(at)", "me@email.com") # me(at)email.com
def dates():
import datetime
today_dt = datetime.datetime.now() # type datetime
today_date = datetime.date.today() # type date
today_date2 = today_dt.date() # datetime to date
print "Current ISO date without time: %s" % today_date # "2016-11-14"
# print month with leading zeros
print "This is month: %s" % today_dt.strftime("%-m") # alternative in print: "%02d"
# custom formatting
# https://docs.python.org/2/library/datetime.html#strftime-and-strptime-behavior
# Fotmats help: http://strftime.org/
print today_dt.strftime("Today is the %d of %b in %y") # Today is the 18 of Nov in 2016
# ISO dates to datetime
import dateutil.parser
# more robust version
dt = dateutil.parser.parse("2016-11-16 09:07:56")
# load from the specific format (format ref: https://docs.python.org/3/library/datetime.html#strftime-and-strptime-behavior)
dt2 = datetime.datetime.strptime("2016-11-16", "%Y-%m-%d" )
print dt # datetime.datetime(2016, 11, 16, 9, 7, 56)
def generate_date_ranges(start_date_str, end_date_str):
"""
In:
Inspired by: http://stackoverflow.com/questions/1060279/iterating-through-a-range-of-dates-in-python
"""
import datetime
from dateutil.rrule import rrule, DAILY
# working with datetime
#https://docs.python.org/2/library/datetime.html
start = datetime.datetime.strptime(start_date_str, "%Y-%m-%d")
end = datetime.datetime.strptime(end_date_str, "%Y-%m-%d")
for dt in rrule(DAILY, dtstart=start, until=end):
print dt.strftime("%Y-%m-%d")
def rpc_dates():
# FRPC datetime: http://fastrpc.sourceforge.net/doc/python/classDateTime.html
# loading datetime using xmlrpclib interface from frpc server returns
# xmlrpclib.DateTime, which internally holds the value in invalid format
# and cannot be used normally (ex. timetuple call)
import xmlrpclib
import fastrpc # fastrpc.sourceforge.net
d_x = xmlrcplib.DateTime("20161115T15:34:53+0100")
print d_x # <DateTime '20161115T15:34:53+0100' at 7f4ab928d248>
print type(d_x) # silly one: <type 'instance'>
print d_x.timetuple() # Traceback: ValueError: unconverted data remains: +0100
# convert to the proper frpc.DateTime
d_f = fastrpc.DateTime(str(d_x))
print d_f # 20161115T15:34:53+0100
print type(d_x) # <type 'DateTime'>
# direct print using fastrpc.Datetime attributes
print "In %d at %02d:%02d" % (df.year, df.hour, df.min) # In 2016 at 15:34
# convert to the datetime
d_dt = datetime.datetime.fromtimestamp(d_f.unixTime)
def paths():
import os
relative_path_to_script = \
os.path.join(os.path.dirname(__file__), '../another-dir/other-file')
def working_with_yaml():
import yaml
# load the config file
try:
with open('/path/to/yaml-file.yml', 'r') as stream:
config = yaml.safe_load(stream)
except IOError as e:
print >> sys.stderr, "Could not open the file: %s" % e
def multiprocessing_worker(worker_id, sleep_seconds):
"""
Worker function for multiprocesing
"""
print "Worker #%s started" % worker_id
import time
time.sleep(sleep_seconds)
msg = "Worker #%s finished" % worker_id
print msg
return msg
def multiprocessing_pool_of_workers():
"""
Multiprocessing Pool.
"""
from multiprocessing import Pool
num_workers = 4
pool = Pool(num_workers)
workers_params = [[worker_id, num_workers-worker_id] for worker_id in range(num_workers)]
results = pool.map(multiprocessing_worker, workers_params)
print results
def multiprocessing_process():
"""
Multiprocessing Pool: https://pymotw.com/2/multiprocessing/basics.html
"""
from multiprocessing import Process
num_workers = 4
jobs = []
for worker_id in range(num_workers):
p = Process(target=multiprocessing_worker, args=(worker_id, num_workers))
jobs.append(p)
p.start()
def random():
import random
random.random() # Random float x, 0.0 <= x < 1.0
random.uniform(1, 10) # Random float x, 1.0 <= x < 10.0
random.randint(1, 10) # Integer from 1 to 10, endpoints included
random.randrange(0, 101, 2) # Even integer from 0 to 100
random.choice('abcdefghij') # Choose a random element
random.choice(['one', 'two', 'three']) # Choose a random element
random.choice(string.ascii_lowercase) # random ASCII lowercase character
random.shuffle([1, 2, 3, 4]) # shuffle the array
random.shuffle([1, 2, 3, 4], 2) # pick two random numbers
def write_file():
# create the testing template
with open('/tmp/pycheat-test-file', 'w') as f:
f.write("test line no.1\ntest line no.2\nline 3\nanother line!")
def read_file():
write_file()
with open('/tmp/pycheat-test-file') as f:
# read all at once to the list of lines
lines_list = f.readlines()
print lines_list
# seek to the file beginging
f.seek(0)
# iterate over lines - do not load the whole file to the memory
print "Iterate over lines:\n--------------"
for line in f:
print line
def file_info():
import os
statinfo = os.stat('somefile.txt')
statinfo.st_size # 926L (size in bytes)
def requests_parsing():
import requests
request_url = "http://testing-page.com/slug/?querystring"
timeout = 2 # seconds
r = requests.get(request_url, timeout=timeout)
if r.status_code != 200:
print "Error in the request: %s" % r
#response attributes: r.headers, r.encoding, r.text, r.json()
def sftp_uploading():
"""
##### TODO
"""
import paramiko
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.load_host_keys(os.path.expanduser(os.path.join("~", ".ssh", "known_hosts")))
ssh.connect(server, username='myuser', password='mypass')
sftp = ssh.open_sftp()
print "Copying %s to %s:%s" % (src, server, dst)
sftp.put(src, dst, callback=None)
sftp.close()
ssh.close()
def rpc():
# XML-RPC = python module for XML-RPC
import xmlrpclib
# Connects. Ignores path after port: http://myserver.dev:1234/IGNORED
server = xmlrpclib.ServerProxy("http://myserver.dev:1234", allow_none=True)
# direct method call
print server.help()
# method name from the variable
method_handler = getattr(server, "myMethod")
params = {
"flag": True,
"mystring": "test",
"date": xmlrpclib.DateTime("2015-04-20")
}
res = method_handler(params)
if res["status"] / 100 != 2:
raise Exception("Invalid RPC call: %s", res)
# FRPC - Seznam.cz module (http://opensource.seznam.cz/frpc/)
# supports numbers > int32
import fastrpc
# Connects. Alternative params: readTimeout (ms), writeTimeout,
# connectTimeout, useBinary=fastrpc.ON_SUPPORT_ON_KEEP_ALIVE
server = fastrpc.ServerProxy("http://myserver.dev:1234/notignored-path")
# the rest is similar to XML-RPC
try:
res = server.myMethod(params)
print "status: %s" % res["status"]
print res
except fastrpc.Fault, e:
print e
except fastrpc.ProtocolError, e:
print e
except fastrpc.StreamError, e:
print e
def jinja():
"""
Jinja2 templating engine usage.
"""
template_path = '/tmp/pycheat-jinja-template.html'
output_path = '/tmp/pycheat-jinja-output.html'
# create the testing template
with open(template_path, 'w') as f:
f.write("""Testing template with {{athlet_type}}:
{% for a in athlets %}
{{a.name}} is from {{a['country']}}
{% endfor %}""")
# testing dict with variables
context = {
'athlet_type': 'tennis players',
'athlets': [
{'name': 'Roger Federer', 'country': 'SUI'},
{'name': 'Rafael Nadal', 'country': 'ESP'},
{'name': 'Novak Djokovic', 'country': 'SRB'}
]
}
import jinja2
import os
# render the template
template_dir, template_filename = os.path.split(template_path)
loader = jinja2.FileSystemLoader(template_dir)
# whitespace control:
# http://jinja.pocoo.org/docs/2.9/templates/#whitespace-control
jinja_env = jinja2.Environment(loader=loader, trim_blocks=True,
lstrip_blocks=True)
template = jinja_env.get_template(template_filename)
rendered_output = template.render(context)
# print and write the result to the file
print rendered_output
with open(output_path, 'w') as f:
f.write(rendered_output.encode('utf-8'))
def main():
print "Main function called"
# argparse example
import argparse
parser = argparse.ArgumentParser(description='Program description which'
' will be displayed in help.',
# ArgumentDefaultsHelpFormatter for displaying
# default values in help
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
# group of exclusive options
sp = parser.add_subparsers(dest='subparser_value')
sp.add_parser('start', help='Starts %(prog)s daemon')
sp.add_parser('stop', help='Stops %(prog)s daemon')
sp.add_parser('restart', help='Restarts %(prog)s daemon')
# required
parser.add_argument('--host', help='Hostname help text', required=True)
# default
parser.add_argument('--port', help='some help text', default=8080)
# options (possibilities)
parser.add_argument('--protocol', help='some help text', choices=['http', 'https'])
# type setting
parser.add_argument('--workers', help='Number of parallel workers', type=int, default=1)
# bool value (store_true sets True when flag is present and it omits the value;
# similar is store_false and store_const)
parser.add_argument('--dry-run', help='This is bool flag', action='store_true')
# parse arguments (in case of invalid input, it prints the help and exits)
args = parser.parse_args() # accessing: args.myparam
# you cannot use reserved keywords like "from" (ex. args.from) - in that case you need a dict:
# source: https://parezcoydigo.wordpress.com/2012/08/04/from-argparse-to-dictionary-in-python-2-7/
opts = vars(args) # accessing: opts['myparam']
print "Current mode: %s" % opts['subparser_value']
if opts['subparser_value'] == 'start':
print "Yes, we launched!"
# return codes
import sys
import os
# useful return codes: (more on https://docs.python.org/3/library/os.html#os._exit)
# EX_OK
# EX_USAGE - bad program usage
# EX_DATAERR - incorrect input data
# EX_NOINPUT - input file does nor exist or is not readable
# EX_OSFILE - file does nor exist or is not readable
# EX_IOERR - generic IO error
# EX_UNAVAILABLE - required service is unavailable
# EX_SOFTWARE - software runtime error
# EX_CONFIG - configuration error
# EX_NOTFOUND
sys.exit(os.EX_OK)
if __name__ == '__main__':
main()