Skip to content

Instantly share code, notes, and snippets.

@psycho23
Last active July 23, 2017 03:58
Show Gist options
  • Save psycho23/e265e53517e2b529b0f406be731f31a9 to your computer and use it in GitHub Desktop.
Save psycho23/e265e53517e2b529b0f406be731f31a9 to your computer and use it in GitHub Desktop.
String fxns (in,not in)
from str (.center,.count,.find,.index,.isdigit,.isalpha,
.join,len,.ljust,.lstrip,.rjust,.split)
Array/List fxns (.append,.clear,.copy,.count,.extend,in,.index,
.insert,list,not in,.pop,.remove,.reverse,.sort)
Dict/Hash fxns (.values,.keys,in,not in)
mydict = { 1:'sup', 's':'a value' }
Variable fxns (False,int,is,None,range,repr,str,True,type)
Math fxns (**,pow,round)
from float (.is_integer)
from math import (ceil,degrees,*e,fabs,floor,*pi,radians,sqrt)
from os import unlink,stat
from random import choice,gauss,randint,shuffle
from re (.compile) #search compile on this page.
from struct import pack, unpack
#---------------------------------------------------------------------------------
#DateTime shit. pydoc datetime
import datetime
print(datetime.datetime.now())
from datetime import datetime, date, time, timedelta
datetime.now()
dt = datetime.strptime("21/11/06 16:30", "%d/%m/%y %H:%M")
tt = dt.timetuple()
for it in tt:
print(it)
dt -= timedelta(hours=8)
dt.strftime('%b %d. %I:%M %p') #Jul 04. 06:01 PM
#---------------------------------------------------------------------------------
https://docs.python.org/3/library/pathlib.html#module-pathlib
https://docs.python.org/3/library/os.path.html#module-os.path
get_file_last_modified('/etc/bashrc')
is_regular_file?()
is_symbolic_link?()
pydoc os
cd() #chdir()
chmod()
chown() #pydoc shutil.chown()
cp() #pydoc shutil.copyfile()
cp-R() #pydoc shutil.copytree()
df() #pydoc shutil.disk_usage('/')
env() #getenv('PATH')
file_get_last_access_timestamp()
file_get_last_change/mod_timestamp()
file_get_size()
file_get_temp() #tempnam(), tmpfile(), tmpnam()
#pydoc tempfile
file_get_owner()
file_make_archive() #pydoc shutil.make_archive
file_open() #open()
file_read_line-by-line() #pydoc fileinput
file_seek(f,0) #go back to the beginning...bitch.
ln(target, name) #symlink()
ls() #aka. dir, listdir. show directory/folder contents
mkdir() #aka. makedirs()
mv() #aka. rename()
process_kill(ID, SIG) #kill_process
process_get_owner #geteuid() get THIS process.
ps_user_owner() #(current processes)
pwd() #getcwd()
rm() #aka. remove(), removedirs(), rmdir()
rm-fr() #pydoc shutil.rmtree()
system() #`echo sup`
touch() #aka. utime(). set times for file.
username() #getlogin() get current user's login name.
which() #pydoc shutil.which(str_cmd)
#---------------------------------------------------------------------------------
print(r'\n\n\n\n')#\n\n\n\n gets printed.
print('\n\n\n\n') #these last two are the same.
print("\n\n\n\n")
import random
l = [1,2,3,4,5]
random.shuffle(l)
print(l)
mydict = {1:'sup', '2':'value'}
print(repr(mydict)) #{1: 'sup', '2': 'value'}
if '1' in mydict: #NO
print(mydict['1'])
if 2 in mydict: #NO
print(mydict[2])
f = open('readthis.txt') #default is reading
ar = f.readlines()
print(repr(sz)) #['line 1\n', 'line 2\n', 'line 3']
ar.append("\na new line bitch")
f.close()
f = open('readthis.txt', 'w') #'w+' or 'a' to auto-create
f.writelines(ar) #'w+' auto-purges
if ! False:
print('syntax error')
if not False:
print('this works')
print('hello'.isalpha()) #True
print('hello'.isdigit()) #False
print(type((1,2,3))) #<class 'tuple'>
print(type([1,2,3])) #<class 'list'>
for i in range(0, 3, 3): #increment by 3
print(i) #0
for i in range(0, 4, 3): #increment by 3
print(i) #0.3.
from random import randint,choice
myar = ('q','w','e','r','t','y')
for i in range(1,2): #1
print(randint(5,6)) #5 or 6
print(choice(myar))
ar = (1, 2, 3)
print(ar * 3) #(1,2,3,1,2,3,1,2,3)
ar = [1, 2, 3]
print(ar * 3) #[1,2,3,1,2,3,1,2,3]
print(repr('sup\n')) #'sup\n'
print(type('sup\n')) #<class 'str'>
python -c "print('hello'.rjust(10))"
# hello
#string slice/substring
v = 'sup'
v[-1:] #'p'
v[-2:-1] #'u'
i = int(input('Enter a number: '))
if i == 0:
print('found 0')
elif i == 1:
print('found 1')
else:
print('found unknown')
myString = 'Y'
if myString.lower() in ('y', 'yes'):
print('You have won!')
import re
print(re.compile('my?reg', re.IGNORECASE).search('abcdefghijklmregno'))
#<_sre.SRE_Match object; span=(12, 16), match='mreg'>
https://docs.python.org/3/library/re.html#match-objects
hello = False or True
print(hello,1,2,3, end="", sep="") #True123
def fib(num):
x, y = 0, 1
while x < num:
print(x, end=' ', sep='', flush=True)
x, y = y, x+y
print()
fib(200) #0 1 1 2 3 5 8 13 21 34 55 89 144
mylist = ('column 1', 'column 2', 'column 3', 'column 4');
print(' | '.join(mylist[-2:]));
'''This is a comment.
multi-lined :D
'''
def subroutine_name(name=None, whatever=None):
if name:
print('your name must be ' + str(name) + '. Interesting.')
ar1 = [1,2,3] #list
ar2 = (1,2,3) #tuple
ar1.insert(0, 'first')
#ar2.insert(0, 'first') ERROR
print(ar1)
myar = [1,2,3,4,5,6]
del myar[0] #0=>1
del myar[0:1] #0=>None, 1=>2
print(myar) #[3,4,5,6]
del myar[:] #now an empty list. (myar.clear())
del myar #can never access myar again.
myar = [1,2,2,3,3,3]
myar.count(3) #3 IE. "count how many times this value is in the array"
myar = [1,2,3,4,5,6]
del myar[0]
myar.insert(0,1)
del myar[0]
print(myar) #2,3,4,5,6
print(myar.index(6)) #IE. "find location where value=6"
#=4
myar = [0,1,2,3,4,5,6]
myar.pop(2)
print(myar) #[0,1, 3,4,5,6]
myar = [0,1]
myar.extend((2,3)) #IE. "extend the list/array with these fresh values"
myar[len(myar):] = (4,5,6)#IE. (samething)
print(myar)
#does it look like a number?
import numbers
ar = (1,'sz',2.6,'sz3333',3)
for value in ar:
if isinstance(value, numbers.Number):
print(str(value) + ' looks like a number')
else:
print(value + ' looks like a string')
(6.11111).is_integer() # False.
for value in b'to integers and beyond!':
print(value)
for n in range(5): #0..4
print(n)
for n in range(1,10): #1..9
print(n)
ar = list(range(5))
ar.insert(0, 'first')
print(ar)
key = True
value = False
assert(key is not False and value is not True)
v = 'hello'
assert isinstance(v, str)
assert v.replace('.', '').replace('-', '').replace('_', '').isalnum()
print chr(97) #'a'
from struct import unpack
print(unpack('c', bytes(chr(97), 'ASCII'))) #(b'a',)
var = None
if var == None:
print('nothing')
if var is None: #preferred!
print('still nothing')
keys = (1,3,2,4)
for k in keys:
print(k)
continue
break
#like Data::Dumper() and crazzzy syntax :D
myar = [1,
2,3,4,[1],[],
3]
print(myar)
mystring = 'hello %d' % 500
print(mystring)
mystring = 'hello {}'.format(501)
print(mystring)
mystring = ('concat'
'this and' 'this')
print(mystring)
if 0 == '':
print('sup1')
elif '' == False:
print('sup1.5')
elif 0 == False:
print('sup2') #DING!!!
else:
print('sup3')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment