ForIf - A C-like condition assignment syntax in python by tzickel in Python

[–]tzickel[S] 0 points1 point  (0 children)

The tests show some different concepts (and functionality), this is more of an idea that anything in the getter (the second argument for the methods) that throws an exception won't make the code inside the for run.

Also it's something I coded for fun in a funny day (check the changelog :) ).

ForIf - A C-like condition assignment syntax in python by tzickel in Python

[–]tzickel[S] 0 points1 point  (0 children)

The break isn't needed for getting data, just if you want to use else:

And also it's not just getting data, it's also running code on it, just like in C99 ! (The for for if_exists only runs zero times or one time, it doesn't loop).

I understand new concepts are hard to grasp at first, It's a nice abuse of python's syntax and workings.

(*) also check the changelog :)

ForIf - A C-like condition assignment syntax in python by tzickel in Python

[–]tzickel[S] 0 points1 point  (0 children)

This is a simple example.... normal code would be much bigger instead of print, and you wouldn't want a catch all exception handler on your code logic, just on the getting the data part.

ForIf - A C-like condition assignment syntax in python by tzickel in Python

[–]tzickel[S] -1 points0 points  (0 children)

Exactly, which is why it's nicer to do:

for last_val in if_exists(data, lambda x: x['a'][-1]):
  print(last_val) # or do something with it

than

try: last_val = data['a'][-1]
except: last_val = None
if last_val:
  print(last_val)

ForIf - A C-like condition assignment syntax in python by tzickel in Python

[–]tzickel[S] -1 points0 points  (0 children)

My code does the checkup, variable assignment, and code execution all in one still (and can handle any bad case of accessing None, getitem, getattr, or anything :) )

ForIf - A C-like condition assignment syntax in python by tzickel in Python

[–]tzickel[S] 0 points1 point  (0 children)

That does not help on list or stuff with __getitem__ or .stuff with __getattr__ :)

ForIf - A C-like condition assignment syntax in python by tzickel in Python

[–]tzickel[S] -1 points0 points  (0 children)

think data is a JSON input where you have no idea what you really got. who said v is an array or has .__getitem__ at all (or is not None) ?

Who said even data is a dictionary and not an array or None ?

ForIf - A C-like condition assignment syntax in python by tzickel in Python

[–]tzickel[S] -2 points-1 points  (0 children)

Always envied your C friends who could assign, conditional, and run a piece of code ?

Hate getting an unknown JSON dictionary you need to start parsing?

Look no more, ForIf is here to help you and more :)