Last active
March 20, 2024 22:23
-
-
Save nafeesb/8e3d5f74cf4c4e6e5dbcc2662426bade to your computer and use it in GitHub Desktop.
Revisions
-
nafeesb revised this gist
Mar 20, 2024 . 1 changed file with 3 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,3 +1,6 @@ ## Script dir `os.path.dirname(os.path.realpath(__file__))` ## Clear tqdm error `tqdm._instances.clear()` -
nafeesb revised this gist
Mar 19, 2024 . 1 changed file with 7 additions and 4 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -106,9 +106,12 @@ Actions.run.value # index ## Get IP address ```python def get_IP_address(): import socket s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) s.connect(('www.apple.com', 80)) # Connect to LDAP. This does not actually need to route. ipaddr = s.getsockname()[0] s.close() return ipaddr ``` -
nafeesb revised this gist
Mar 17, 2024 . 1 changed file with 8 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -104,3 +104,11 @@ Actions.run.name # string name Actions.run.value # index ``` ## Get IP address ```python import socket s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) s.connect(('www.google.com', 80)) s.getsockname()[0] ``` -
nafeesb revised this gist
May 2, 2023 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -95,12 +95,12 @@ for dll in p.memory_maps(): ``` ## Dynamic enums ```python actstr = 'stand,walk,run,sit' Actions = Enum('Actions', { v:i for i,v in enumerate(actstr.split(',')) }) Actions.run Actions['run'] Actions.run.name # string name Actions.run.value # index ``` -
nafeesb revised this gist
May 2, 2023 . 1 changed file with 10 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -94,3 +94,13 @@ for dll in p.memory_maps(): 6 RETURN_VALUE ``` ## Dynamic enums ``` actstr = 'stand,walk,run,sit' Actions = Enum('Actions', { v:i for i,v in enumerate(actstr.split(',')) }) Actions.run Actions['run'] Actions.run.name # string name Actions.run.value # index -
nafeesb revised this gist
Nov 8, 2022 . 1 changed file with 15 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -79,3 +79,18 @@ for dll in p.memory_maps(): print(dll.path) ``` ## Disassemble expression ```python >>> import dis >>> dis.dis("not x is None") 1 0 LOAD_NAME 0 (x) 2 LOAD_CONST 0 (None) 4 COMPARE_OP 9 (is not) 6 RETURN_VALUE >>> dis.dis("x is not None") 1 0 LOAD_NAME 0 (x) 2 LOAD_CONST 0 (None) 4 COMPARE_OP 9 (is not) 6 RETURN_VALUE ``` -
nafeesb revised this gist
Feb 2, 2021 . 1 changed file with 1 addition and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -38,6 +38,7 @@ result.names = [ l[6] for l in buf ] ## First item in generator `it = next(x for x in loader)` `item = next(iter(themap.keys()))` ## Interrupt tqdm ```python -
nafeesb revised this gist
Feb 11, 2020 . 1 changed file with 3 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,3 +1,6 @@ ## Clear tqdm error `tqdm._instances.clear()` ## Extend namedtuple with methods ```python ExperimentProps = namedtuple( 'ExperimentProps', config.keys() ) -
nafeesb revised this gist
Jan 16, 2020 . 1 changed file with 2 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -21,6 +21,8 @@ class Experiment(ExperimentProps): with open(self.skeleton, 'r') as f: skel = yaml.load(f, Loader=yaml.FullLoader) return skel result = Experiment( *config.values() ) ``` ## Simple struct -
nafeesb revised this gist
Sep 26, 2019 . 1 changed file with 33 additions and 24 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,3 +1,36 @@ ## Extend namedtuple with methods ```python ExperimentProps = namedtuple( 'ExperimentProps', config.keys() ) class Experiment(ExperimentProps): @property def root_path(self): return self._root_path @root_path.setter def root_path(self, _val): if os.path.exists(_val): self._root_path = _val def network_path(self): return os.path.join( self._root_path, 'network', self.name) def get_skeleton(self): ''' Load the skeleton, and return dict ''' with open(self.skeleton, 'r') as f: skel = yaml.load(f, Loader=yaml.FullLoader) return skel ``` ## Simple struct ```python result = SimpleNamespace() result.sizes = [ int(l[3]) for l in buf ] result.types = [ l[5] for l in buf ] result.names = [ l[6] for l in buf ] ``` ## First item in generator `it = next(x for x in loader)` @@ -40,27 +73,3 @@ for dll in p.memory_maps(): print(dll.path) ``` -
nafeesb revised this gist
Sep 26, 2019 . 1 changed file with 25 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -38,4 +38,29 @@ import psutil, os p = psutil.Process( os.getpid() ) # current proc for dll in p.memory_maps(): print(dll.path) ``` ## extend namedtuple with methods ```python ExperimentProps = namedtuple( 'ExperimentProps', config.keys() ) class Experiment(ExperimentProps): @property def root_path(self): return self._root_path @root_path.setter def root_path(self, _val): if os.path.exists(_val): self._root_path = _val def network_path(self): return os.path.join( self._root_path, 'network', self.name) def get_skeleton(self): ''' Load the skeleton, and return dict ''' with open(self.skeleton, 'r') as f: skel = yaml.load(f, Loader=yaml.FullLoader) return skel ``` -
nafeesb revised this gist
Jun 13, 2019 . 1 changed file with 12 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,6 +1,18 @@ ## First item in generator `it = next(x for x in loader)` ## Interrupt tqdm ```python try: with tqdm(...) as t: for i in t: ... except KeyboardInterrupt: t.close() raise t.close() ``` ## Merge PDF files ```python import PyPDF2 -
nafeesb revised this gist
Jun 12, 2019 . 1 changed file with 3 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,3 +1,6 @@ ## First item in generator `it = next(x for x in loader)` ## Merge PDF files ```python import PyPDF2 -
nafeesb revised this gist
Apr 17, 2019 . 1 changed file with 1 addition and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -17,6 +17,7 @@ reduce( lambda x,y: x+y, l ) ``` ## List DLLs loaded by a process From: [StackOverflow](https://stackoverflow.com/questions/5553917/how-to-list-all-dlls-loaded-by-a-process-with-python) ```python import psutil, os p = psutil.Process( os.getpid() ) # current proc -
nafeesb revised this gist
Apr 17, 2019 . 1 changed file with 8 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -15,3 +15,11 @@ from functools import reduce l = [['a', 'b', 'c'], ['d'], ['e', 'f']] reduce( lambda x,y: x+y, l ) ``` ## List DLLs loaded by a process ```python import psutil, os p = psutil.Process( os.getpid() ) # current proc for dll in p.memory_maps(): print(dll.path) ``` -
nafeesb revised this gist
Mar 27, 2019 . 1 changed file with 8 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -7,4 +7,11 @@ merger = PyPDF2.PdfFileMerger() for f in tomerge: merger.append(PyPDF2.PdfFileReader( open(f, 'rb') ) ) merger.write('merged.pdf') ``` ## Flatten 2-level list ```python from functools import reduce l = [['a', 'b', 'c'], ['d'], ['e', 'f']] reduce( lambda x,y: x+y, l ) ``` -
nafeesb created this gist
Feb 24, 2019 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,10 @@ ## Merge PDF files ```python import PyPDF2 tomerge = [ v for v in os.listdir('.') if 'pdf' in v ] merger = PyPDF2.PdfFileMerger() for f in tomerge: merger.append(PyPDF2.PdfFileReader( open(f, 'rb') ) ) merger.write('merged.pdf') ```