Last active
March 21, 2020 11:55
-
-
Save bigsnarfdude/46036cf223a7d1b78b681ae19389d1c8 to your computer and use it in GitHub Desktop.
formaters
This file contains 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 characters
def format_dict_get(my_dict, location, key): | |
def zero_none(x): | |
return x or 0 | |
return zero_none(my_dict.get(location).get(key)) | |
# clean any of the dicts for None values | |
for k, v in query_dict.items(): | |
for k2, v2 in v.items(): | |
if v2 is None or v2 is False: | |
query_dict[k][k2] = 0 | |
def neg_formatter(x): | |
""" | |
Takes positive or negative number and returns it accounting style | |
""" | |
import string | |
class NegativeParenFormatter(string.Formatter): | |
def format_field(self, value, format_spec): | |
try: | |
if value<0: | |
return "(" + string.Formatter.format_field(self, -value, format_spec) + ")" | |
else: | |
return string.Formatter.format_field(self, value, format_spec) | |
except: | |
return string.Formatter.format_field(self, value, format_spec) | |
f = NegativeParenFormatter() | |
x=0 if x == False or x == np.nan or x is None else x | |
return f.format("{0:,}", round(x, 2)) | |
def neg_formatter2(x): | |
""" | |
Takes positive or negative number and returns it accounting style | |
""" | |
import string | |
class NegativeParenFormatter(string.Formatter): | |
def format_field(self, value, format_spec): | |
try: | |
if value<0: | |
return "(" + string.Formatter.format_field(self, -value, format_spec) + ")" | |
else: | |
return string.Formatter.format_field(self, value, format_spec) | |
except: | |
return string.Formatter.format_field(self, value, format_spec) | |
f = NegativeParenFormatter() | |
x=0 if x == False or x == np.nan or x is None else x | |
return f.format("{0:,}", int(round(x))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment