Skip to content

Commit 05b2549

Browse files
committed
0.0.5 - new conversions, documentation
1 parent f6629b1 commit 05b2549

File tree

5 files changed

+82
-11
lines changed

5 files changed

+82
-11
lines changed

README.md

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ Unofficial Quickbase JSON API wrapper for python
88

99
Makes life a little easier!
1010

11-
## Quickstart
11+
# Quickstart
1212

13-
### Installation
13+
## Installation
1414
To install, run `pip install quickbase-json-api-client`
1515

16-
### Initialize Client
16+
## Initialize Client
1717
Use the following code to create and initialize a client object.
1818
```
1919
from quickbase_json.client import QuickbaseJSONClient # import client
@@ -24,7 +24,7 @@ client = QuickbaseJSONClient(realm="yourRealm", auth="userToken")
2424
Where `yourRealm` is the name (subdomain) of your Quickbase Realm and `userToken` is the user token used to authenticate
2525
with the realm.
2626

27-
### Query Records
27+
## Query Records
2828
Querying for records is one of the most useful features of the Quickbase JSON API. Querying records with QJAC can be done
2929
using the following code
3030

@@ -33,16 +33,28 @@ using the following code
3333
Where `tableId` is the ID of the table you wish to query from, `fids` is a list of field IDs you wish to receive and `queryString`
3434
is a quickbase [query string](https://help.quickbase.com/api-guide/componentsquery.html).
3535

36-
### Response Objects
36+
## Response Objects
3737

3838
A `QBResponse` object is returned when querying records with QJAC. A `QBResponse` has several methods that make
39-
handling returned data easier. Here are a few of the most useful ones:
39+
handling returned data easier. Here are a few of the most useful ones.
4040

41-
**denest()**
41+
### Response Methods
42+
43+
- **denest()**
4244

4345
Denests the data. I.e. changes `{'fid': {'value': 'actualValue'}}` to `{'fid': 'actualValue'}`
4446

45-
**orient(orient='records', key='3')**
47+
- **orient(orient='records', key='3')**
4648

4749
Orients the data. Currently, the only option is 'records'. This will orient the returned data into a "record like structure", i.e. changes
48-
`{'fid': 'actualValue', 'fid': 'actualValue'}` to `{'key': {etc: etc}}`
50+
`{'fid': 'actualValue', 'fid': 'actualValue'}` to `{'key': {etc: etc}}`
51+
52+
- **convert()**
53+
54+
Converts the data, based on fields and provided arguments. For example, calling `convert('datetime')` will convert all data with fields
55+
of the 'date time' type to python datetime objects. Other conversions are 'currency' and 'int'.
56+
57+
- **round_ints()**
58+
59+
Rounds all float integers into whole number ints. i.e. converts `55.0` to `55`.
60+

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[metadata]
22
name = quickbase-json-api-client
3-
version = 0.0.3
3+
version = 0.0.5
44
author = Robert Carroll
55
author_email = rob@shenandoah.capital
66
description = Python wrapper for quickbase JSON API.

src/quickbase_json/qb_response.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,9 @@ def convert_type(self, field_type, **kwargs):
238238
"""
239239

240240
def c_datetime():
241-
241+
"""
242+
Helper function that converts date time QB fields to datetime ojects.
243+
"""
242244
for f in self.get('fields'):
243245
fid = str(f.get('id'))
244246
if f.get('type') == 'date time':
@@ -251,7 +253,24 @@ def c_datetime():
251253
d.update({fid: {
252254
'value': datetime.datetime.strptime(d.get(fid).get('value'), '%Y-%m-%dT%H:%M:%S.%fZ')}})
253255

256+
def c_currency(currency_format):
257+
258+
for f in self.get('fields'):
259+
fid = str(f.get('id'))
260+
if f.get('type') == 'numeric currency':
261+
262+
if 'denest' in self.operations:
263+
for d in self.get('data'):
264+
fmt = currency_format + "{:,.2f}".format(d.get(fid))
265+
d.update({fid: fmt})
266+
else:
267+
for d in self.get('data'):
268+
fmt = currency_format + "{:,.2f}".format(d.get(fid).get('value'))
269+
d.update({fid: {'value': fmt}})
270+
254271
if field_type == 'datetime':
255272
c_datetime()
273+
if field_type == 'numeric currency' or field_type == 'currency':
274+
c_currency(kwargs.get('fmt'))
256275

257276
return self

tests/sample_data.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,31 @@
6161
skip 0
6262

6363
"""
64+
65+
record_data_currency = {
66+
"data": [
67+
{
68+
"6": {
69+
"value": 55.55
70+
},
71+
},
72+
{
73+
"6": {
74+
"value": 13
75+
},
76+
}
77+
],
78+
"fields": [
79+
{
80+
"id": 6,
81+
"label": "Cost",
82+
"type": "numeric currency"
83+
}
84+
],
85+
"metadata": {
86+
"totalRecords": 2,
87+
"numRecords": 1,
88+
"numFields": 1,
89+
"skip": 0
90+
}
91+
}

tests/test_response.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,18 @@ def test_convert_datetime():
4848
assert isinstance(dt_test2, datetime.datetime)
4949

5050

51+
# test currency convert
52+
def test_convert_currency():
53+
# test datetime convert w/o denest
54+
res = QBResponse('records', sample_data=deepcopy(sample_data.record_data_currency))
55+
res.convert_type('numeric currency', fmt='$')
56+
res.denest()
57+
58+
# make sure both are formatted correctly
59+
assert res.data()[0].get('6') == '$55.55'
60+
assert res.data()[1].get('6') == '$13.00'
61+
62+
5163
# test datetime convert
5264
def test_round_ints():
5365
# test converting ints with floating point

0 commit comments

Comments
 (0)