Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sweep: unify json_name to camel case for python entities #666

Open
5 tasks done
s3341458 opened this issue Sep 20, 2023 · 1 comment · May be fixed by #667
Open
5 tasks done

Sweep: unify json_name to camel case for python entities #666

s3341458 opened this issue Sep 20, 2023 · 1 comment · May be fixed by #667
Labels
sweep Sweep your software chores

Comments

@s3341458
Copy link
Collaborator

s3341458 commented Sep 20, 2023

in sdk/python in some of entities the json_name is lower case like "lower_case" but we want them all camel case like "camelCase"
eg. in sdk/python/payment_devices.py the json_name of PaymentDevice entity is 'paymentDevice' which is correct, however, in sdk/python/email_addresses.py the json name of EmailAddress is lower name.

Now I want you to read all the files in sdk/python if you find json_name of a entity is given as lower case, convert it to camel case.

Checklist
  • python/email_addresses.py ✅ Commit 810a5a2
• Change the `json_name` attribute of the `EmailAddress` class to 'emailAddress'.
  • python/payment_devices.py ❌ Failed
• No changes needed as the `json_name` attribute of the `PaymentDevice` class is already in camel case.
  • python/payments.py ❌ Failed
• Change the `json_name` attribute of the `Payment` class to 'payment'.
  • python/addresses.py ❌ Failed
• Change the `json_name` attribute of the `Address` class to 'address'.
  • python/util/name_protocol.py ❌ Failed
• No changes needed as this file contains utility functions and does not define any entities.
@sweep-from-cheng sweep-from-cheng bot added the sweep Sweep your software chores label Sep 20, 2023
@sweep-from-cheng
Copy link
Contributor

sweep-from-cheng bot commented Sep 20, 2023

Here's the PR! #667.

💎 Sweep Pro: I used GPT-4 to create this ticket. You have unlimited GPT-4 tickets.


Actions (click)

  • Restart Sweep

Step 1: 🔎 Searching

I found the following snippets in your repository. I will now analyze these snippets and come up with a plan.

Some code snippets I looked at (click to expand). If some file is missing from here, you can mention the path in the ticket description.

import sdk.python.entities
from sdk.python.entities import Property
class EmailAddress(sdk.python.entities.Entity):
resource = '/email_addresses/'
json_name = 'email_address'
id = Property(int)
email_address = Property(str)
class EmailAddresses(sdk.python.entities.Resource):
entity_class = EmailAddress
json_name = 'email_addresses'

import sdk.python.entities
from sdk.python.companies import Company
from sdk.python.entities import Property
class PaymentDevice(sdk.python.entities.Entity):
json_name = 'paymentDevice'
resource = '/payment_devices/'
id = Property(int)
code = Property(str)
device_id = Property(str)
square_id = Property(str)
device_data = Property(str)
company = Property(Company, backref="payment_devices")
class PaymentDevices(sdk.python.entities.Resource):
entity_class = PaymentDevice
json_name = 'paymentDevices'

sdk/python/payments.py

Lines 1 to 44 in 5ff083e

import datetime
import sdk.python.entities
import sdk.python.users
from sdk.python.automatic_payment_relationships import \
AutomaticPaymentRelationship
from sdk.python.util.business_default import PAYMENT_TYPES
from sdk.python.entities import Property
class Payment(sdk.python.entities.Entity):
resource = '/payments/'
json_name = 'payment'
def __init__(self):
super(Payment, self).__init__()
self.escape_fields = ['payment_type', 'pay_date']
id = Property(int)
note = Property(str)
payment_type = Property(int)
pay_date = Property(datetime.datetime)
amount = Property(float)
payment_recorder = Property(sdk.python.users.Users)
send_sms = Property(bool)
send_email = Property(bool)
refunded = Property(datetime.datetime)
refund_issuer = Property(sdk.python.users.Users)
auto_refundable = Property(bool)
charged_by_payment_relationship = Property(AutomaticPaymentRelationship)
def payment_type_string(self):
""" Return the string value of payment type from the
sdk.python.util.business_default file
"""
return PAYMENT_TYPES[self.payment_type]
class Payments(sdk.python.entities.Resource):
entity_class = Payment
json_name = 'payments'

sdk/python/addresses.py

Lines 1 to 54 in 5ff083e

import pycountry
import sdk.python.entities
from sdk.python.entities import Property
class Address(sdk.python.entities.Entity):
resource = '/addresses/'
json_name = 'address'
id = Property(int)
line_one = Property(str)
line_two = Property(str)
city = Property(str)
state = Property(str)
country = Property(str)
postcode = Property(str)
def __repr__(self):
return "{},{},{},{},{}".format(self.line_one,
self.city,
self.state,
self.country,
self.postcode)
def clone(self):
""" Return a copy of this address """
clone_address = Address()
for json_name in self.json_properties:
setattr(clone_address, json_name,
(getattr(self, json_name)))
clone_address.id = None # type: ignore
return clone_address
def country_name(self):
""" Convert the country initials into
the country full name and returns the country
full name
"""
country_name = self.country
try:
country_name = pycountry.countries.\
get(alpha2=self.country).name
except KeyError:
pass
return country_name
class Addresses(sdk.python.entities.Resource):
entity_class = Address
json_name = 'addresses'

import inflection
import sdk.python.util.rights
CAMEL = 1
UNDERSCORE = 2
VERSION_NUMBER = 6
def camelize(word):
return inflection.camelize(word, uppercase_first_letter=False)
function_dict = {CAMEL: camelize,
UNDERSCORE: inflection.underscore}
def plural_to_singular(word):
return inflection.singularize(word)
def singular_to_plural(word):
return inflection.pluralize(word)
def parse_json_key_names(json_object, standard):
""" Convert the string value of key in json
to a required standard. by the time this comment was written only
camel and underscore were supported
"""
if isinstance(json_object, dict):
old_keys = list(json_object.keys())
for old in old_keys:
new = function_dict[standard](old)
if new != old:
json_object[new] = json_object[old]
del json_object[old]
parse_json_key_names(json_object[new], standard)
else:
parse_json_key_names(json_object[old], standard)
elif isinstance(json_object, list):
for item in json_object:
parse_json_key_names(item, standard)
return json_object
def parse_json_key_underscore(json_object):
return parse_json_key_names(json_object, UNDERSCORE)
def parse_json_key_camel(json_object):
return parse_json_key_names(json_object, CAMEL)
def is_empty_json(json_object):
"""Check whether given json object has value
by checking whether all the values in very key-value
pair of json is None
"""
is_empty = True
if isinstance(json_object, list):
return False
elif isinstance(json_object, dict):
for key, value in json_object.items():
if key != "rights" and is_empty_json(value) is False:
return False
else:
if not empty_content(json_object):
return False
return is_empty
def clean_empty_json(json_object):
"""Give a json which all the empty sub-json
are eliminated
"""
return_json = {}
for key, value in json_object.items():
if not is_empty_json(value):
return_json[key] = value
return return_json
def unpack_recursive_json_iter(json_object, version):
"""Change nested json to a flat json
by using a following rule:
{users:{person:james}} =>
{users-0-person:james, users-count: 1}
{users:[b,c]} => {users-count:2, users-0:b, users-1:c }
so a combined example may be
{users:[{name:james},{name:daniel}]} =>
{users-count:2, users-0-name:james, users-1-name:daniel}
"""
return_json = {}
json_object = clean_empty_json(json_object)
def key_process(key):
if version < 4:
return plural_to_singular(key)
return key
for key, value in json_object.items():
if key == sdk.python.util.rights.Rights.json_name:
continue
# if the value is a list of objects
if isinstance(value, list):
object_in_json_list = value
index = 0
for object_in_json in object_in_json_list:
if isinstance(object_in_json, dict):
for k, v in object_in_json.items():
key_in_return_json = key_process(key) + "-" +\
str(index) + "-" + k
return_json[key_in_return_json] = v
else:
key_in_return_json = key_process(key) + "-" +\
str(index)
return_json[key_in_return_json] = object_in_json
index += 1
count_key = key_process(key) + "-count"
return_json[count_key] = index
elif isinstance(value, dict):
for k, v in value.items():
key_in_return_json = key_process(key) + "-0-" + k
return_json[key_in_return_json] = v
count_key = key_process(key) + "-count"
return_json[count_key] = 1
else:
return_json[key] = value
return return_json
def is_json_flat(json_object):
for value in list(json_object.values()):
if isinstance(value, (dict, list)):
return False
return True
def multidict_to_json(multidict):
""" Return the dictionary version of the flask multidict """
return dict((key, multidict.getlist(key)
if len(multidict.getlist(key)) > 1 else multidict.get(key))
for key in multidict.keys())
def unpack_recursive_json(json_object, version_number):
while not is_json_flat(json_object):
json_object = unpack_recursive_json_iter(json_object, version_number)
return json_object
def empty_content(content):
return content is None
def encode_backend_data(data_dict):
""" Encode a python dictionary to be sent to a backend view """
data_json = parse_json_key_camel(data_dict)


Step 2: ⌨️ Coding

  • python/email_addresses.py ✅ Commit 810a5a2
• Change the `json_name` attribute of the `EmailAddress` class to 'emailAddress'.
  • python/payment_devices.py ❌ Failed
• No changes needed as the `json_name` attribute of the `PaymentDevice` class is already in camel case.
  • python/payments.py ❌ Failed
• Change the `json_name` attribute of the `Payment` class to 'payment'.
  • python/addresses.py ❌ Failed
• Change the `json_name` attribute of the `Address` class to 'address'.
  • python/util/name_protocol.py ❌ Failed
• No changes needed as this file contains utility functions and does not define any entities.

Step 3: 🔁 Code Review

I have finished reviewing the code for completeness. I did not find errors for sweep/unify-json-name-camel-case.

.


🎉 Latest improvements to Sweep:


💡 To recreate the pull request edit the issue title or description. To tweak the pull request, leave a comment on the pull request.
Join Our Discord

@sweep-from-cheng sweep-from-cheng bot linked a pull request Sep 20, 2023 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
sweep Sweep your software chores
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant