PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0
Showing posts with label functools. Show all posts
Showing posts with label functools. Show all posts

Wednesday, May 18, 2022

[FIXED] how to tell if a partial has all arguments satisfied

 May 18, 2022     functools, partial, python     No comments   

Issue

if I'm building a function call using partial, is there any way to query the partial to see if all the arguments for that function have been supplied? For instance in the code below is there any function I can pass empty_partial, partial_partial, and full_partial into, that will return True only on full_partial, indicating that all the arguments have been supplied?

from functools import partial

def foo(bar, baz):
    return bar + baz

empty_partial = partial(foo)
partial_partial = partial(empty_partial, bar=3)
full_partial = partial(partial_partial, baz=5)

print(full_partial())
# 8

if I tried to call the empty partial I'll get a message like this:

empty_partial = partial(foo)
empty_partial()
TypeError: foo() missing 2 required positional arguments: 'bar' and 'baz'

Where do I go to get this error, that it's missing 2 required arguments if I did call it (without having to try to call it and parse the error)?


Solution

get the signature of the partial object, then check to make sure signature.bind does not raise a TypeError

import inspect

def partial_satisfied(partial_fn):
    signature = inspect.signature(partial_fn.func)
    try:
       signature.bind(*partial_fn.args, **partial_fn.keywords)
       return True
    except TypeError:
       return False

should do the trick (requires a python new enough to support function signatures)



Answered By - Mark Harviston
Answer Checked By - Clifford M. (PHPFixing Volunteer)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

[FIXED] how to run nested partials

 May 18, 2022     functools, partial, python     No comments   

Issue

I have a set of nested partials that I'm trying to call:

print(my_partial)

functools.partial(<function g at 0x000001A047370598>,
    functools.partial(<function f at 0x000001A047370620>, 
        functools.partial(<function c at 0x000001A047370400>, 5)))

so when I try to run my partial I get the inner partial back:

print(my_partial)

functools.partial(<function f at 0x000001A047370620>, 
    functools.partial(<function c at 0x000001A047370400>, 5))

(Or sometihng like that). Anyway, so to run this to get the final transformation on 5 (the input data), I have to do this:

print(my_partial()()()())

25

Is there a functools function that I can pass this nested partials to so that it'll just run it all for me? Something like this:

print(functools.run_partials(my_partial))

25

Does something like this exist? I hope so, the soltion I'm working on is buggy:

def run_nested(x):
    print(str(type(x())))
    if 'functools.partial' in str(type(x())):
        run_nested(x())
    print('DONE!', x())
    return x()

Solution

I don't think there's anything in functools to help. You could always keep calling it until it's not callable anymore with a single while. Something like:

from functools import  partial

def f(fn):
    return fn

def g(fn):
    return fn

def c(n):
    return n*n

my_f = partial(g, partial(f, partial(c, 5) ))

print(my_f())
# functools.partial(<function f at 0x10eb7cd08>, functools.partial(<function c at 0x10eb7c598>, 5))

res = my_f
while(callable(res)):
    res = res()
print(res) # '25'


Answered By - Mark
Answer Checked By - Clifford M. (PHPFixing Volunteer)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Older Posts Home
View mobile version

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
All Comments
Atom
All Comments

Copyright © PHPFixing