Skip to content

Instantly share code, notes, and snippets.

@nafeesb
Last active March 20, 2024 22:23
Show Gist options
  • Select an option

  • Save nafeesb/8e3d5f74cf4c4e6e5dbcc2662426bade to your computer and use it in GitHub Desktop.

Select an option

Save nafeesb/8e3d5f74cf4c4e6e5dbcc2662426bade to your computer and use it in GitHub Desktop.

Revisions

  1. nafeesb revised this gist Mar 20, 2024. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions python.md
    Original 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()`

  2. nafeesb revised this gist Mar 19, 2024. 1 changed file with 7 additions and 4 deletions.
    11 changes: 7 additions & 4 deletions python.md
    Original file line number Diff line number Diff line change
    @@ -106,9 +106,12 @@ 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]
    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
    ```

  3. nafeesb revised this gist Mar 17, 2024. 1 changed file with 8 additions and 0 deletions.
    8 changes: 8 additions & 0 deletions python.md
    Original 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]
    ```

  4. nafeesb revised this gist May 2, 2023. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions python.md
    Original 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
    ```

  5. nafeesb revised this gist May 2, 2023. 1 changed file with 10 additions and 0 deletions.
    10 changes: 10 additions & 0 deletions python.md
    Original 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
  6. nafeesb revised this gist Nov 8, 2022. 1 changed file with 15 additions and 0 deletions.
    15 changes: 15 additions & 0 deletions python.md
    Original 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
    ```

  7. nafeesb revised this gist Feb 2, 2021. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions python.md
    Original 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
  8. nafeesb revised this gist Feb 11, 2020. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions python.md
    Original 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() )
  9. nafeesb revised this gist Jan 16, 2020. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions python.md
    Original 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
  10. nafeesb revised this gist Sep 26, 2019. 1 changed file with 33 additions and 24 deletions.
    57 changes: 33 additions & 24 deletions python.md
    Original 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)
    ```

    ## 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
    ```
  11. nafeesb revised this gist Sep 26, 2019. 1 changed file with 25 additions and 0 deletions.
    25 changes: 25 additions & 0 deletions python.md
    Original 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
    ```
  12. nafeesb revised this gist Jun 13, 2019. 1 changed file with 12 additions and 0 deletions.
    12 changes: 12 additions & 0 deletions python.md
    Original 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
  13. nafeesb revised this gist Jun 12, 2019. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions python.md
    Original 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
  14. nafeesb revised this gist Apr 17, 2019. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions python.md
    Original 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
  15. nafeesb revised this gist Apr 17, 2019. 1 changed file with 8 additions and 0 deletions.
    8 changes: 8 additions & 0 deletions python.md
    Original 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)
    ```
  16. nafeesb revised this gist Mar 27, 2019. 1 changed file with 8 additions and 1 deletion.
    9 changes: 8 additions & 1 deletion python.md
    Original 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 )
    ```
  17. nafeesb created this gist Feb 24, 2019.
    10 changes: 10 additions & 0 deletions python.md
    Original 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')
    ```