Skip to content

Instantly share code, notes, and snippets.

@ttsiodras
Last active August 21, 2017 09:48
Show Gist options
  • Select an option

  • Save ttsiodras/88f0afc712b4bfc3fe16 to your computer and use it in GitHub Desktop.

Select an option

Save ttsiodras/88f0afc712b4bfc3fe16 to your computer and use it in GitHub Desktop.

Revisions

  1. ttsiodras revised this gist Jul 24, 2015. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions hack.py
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,4 @@
    #
    # I inherited a large code base, where hundreds of code paths end up
    # calling "common_function_called_in_gazillion_places".
    #
  2. ttsiodras revised this gist Jul 24, 2015. 1 changed file with 14 additions and 1 deletion.
    15 changes: 14 additions & 1 deletion hack.py
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,13 @@
    # I inherited a large code base, where hundreds of code paths end up
    # calling "common_function_called_in_gazillion_places".
    #
    # And the need arose for this function to access the HTTP request's
    # headers...
    #
    # What to do? Refactor all the places leading up to here? In a dynamically
    # typed language, with no compiler to tell us the places to refactor?
    #
    # NO - let's hack the universe instead.

    def get_the_request():
    """
    @@ -23,4 +33,7 @@ def common_function_called_in_gazillion_places( variables, action ):
    if ip_addr not in ['127.0.0.1', 'localhost'] and \
    isinstance(ip_addr, (str,unicode)):
    event_data['context'] = {'ip': ip_addr}
    ....
    ....
    # TADA!!!
    #
    # OK, now I can burn in programmer Hell - for all eternity.
  3. ttsiodras revised this gist Jul 24, 2015. 1 changed file with 6 additions and 1 deletion.
    7 changes: 6 additions & 1 deletion hack.py
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,9 @@

    def get_the_request() :
    def get_the_request():
    """
    Look up the call stack to see if one of our callers has "self.request"
    (i.e. the Pyramid request) and if so, return it
    """
    for f in inspect.stack():
    if 'self' in f[0].f_locals:
    self = f[0].f_locals['self']
    @@ -10,6 +14,7 @@ def get_the_request() :

    def common_function_called_in_gazillion_places( variables, action ):
    ...
    # Get the request from our callers, and extract the IP from it
    request = get_the_request()
    if request is not None:
    ip_addr = request.remote_addr
  4. ttsiodras revised this gist Jul 24, 2015. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion hack.py
    Original file line number Diff line number Diff line change
    @@ -8,7 +8,6 @@ def get_the_request() :
    else:
    return None

    @staticmethod
    def common_function_called_in_gazillion_places( variables, action ):
    ...
    request = get_the_request()
  5. ttsiodras revised this gist Jul 24, 2015. No changes.
  6. ttsiodras revised this gist Jul 24, 2015. No changes.
  7. ttsiodras created this gist Jul 24, 2015.
    22 changes: 22 additions & 0 deletions hack.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,22 @@

    def get_the_request() :
    for f in inspect.stack():
    if 'self' in f[0].f_locals:
    self = f[0].f_locals['self']
    if hasattr(self, 'request'):
    return self.request
    else:
    return None

    @staticmethod
    def common_function_called_in_gazillion_places( variables, action ):
    ...
    request = get_the_request()
    if request is not None:
    ip_addr = request.remote_addr
    if 'X-Forwarded-For' in request.headers:
    ip_addr = request.headers['X-Forwarded-For']
    if ip_addr not in ['127.0.0.1', 'localhost'] and \
    isinstance(ip_addr, (str,unicode)):
    event_data['context'] = {'ip': ip_addr}
    ....