Skip to main content

ssutils

collection of useful python functions

Screenwriter

Use Screenwriter to prefix screen prints

Examples:

1 - Default prefix

from ssutils import Screenwriter

sw = Screenwriter ()
sw.echo ('my output')
Output:
2019-07-26-11:16:04 my output

2 - Date Time parts in prefix

from ssutils import Screenwriter

sw = Screenwriter ('%Y-%m-%d %H:%M:%S.%f ')
sw.echo ('my output')
Output:
2019-07-26 11:16:04 my output

3 - Error, Warning & Info standard prefixes

from ssutils import Screenwriter

sw = Screenwriter ()
sw.error ('an error message')
sw.warn  ('a warming message')
sw.info  ('an informational message')
Output:
2019-07-29-11:39:00 ERROR: an error message
2019-07-29-11:39:00 WARN:  a warming message
2019-07-29-11:39:00 INFO:  an informational message

4 - Trimming content length

By default, log strings are trimmed to 1000 chars. You can change this setting:

from ssutils import Screenwriter

sw = Screenwriter ()
sw.set_maxlen (80) #Set maximum length to 80

For format options, see http://strftime.org/

Sfdc

Use Sfdc to Query SFDC.

Examples:

1 - List Objects

from ssutils import Sfdc

sf = Sfdc('userid', 'password', 'token', False) # last Param turns off verbose
sf.connect ()
sf.load_metadata ()
for apin in sf.object_labels.keys():
        print ("API Name [" + apin + "], Label [" + sf.object_labels[apin] + "]")
for apin in sf.standard_object_names:
        print ("Standard Object [" + apin + "]")
for apin in sf.custom_object_names:
        print ("Custom Object [" + apin + "]")
for apin in sf.custom_setting_names:
        print ("Custom Setting [" + apin + "]")

2 - Describe Objects

from ssutils import Sfdc
sf = Sfdc('userid', 'password', 'token')
sf.connect ()
sf.load_metadata ()

def print_line (a, b, c, d, e):
        s = '{:7}'.format(a) + '{:50}'.format(b)
	s += '{:20}'.format(c) + '{:20}'.format(d) + e
        print (s)

print_line ('Seq', 'API Name', 'Type', 'Length', 'Label')
print_line ('---', '--------', '----', '------', '-----')
fn = 1
for fld in sf.describe_object ('Contact'):
	pfx = "#" + str(fn).ljust(3) + " - "
        print_line (pfx, fld['name'], fld['type'], str(fld['length']), fld['label'])
        fn += 1

3 - Forex Rate Conversions

from ssutils import Sfdc
sf = Sfdc('userid', 'password', 'token')
sf.connect ()

sf.load_fx_rates ()
print (sf.get_amount_in_USD (999, 'EUR'))

4 - Querying from a table based on passed filters

from ssutils import Sfdc
sf = Sfdc('userid', 'password', 'token')
sf.connect ()

tcols = ['Id', 'Account.Name']
conds = []
conds.append ("CloseDate >= " + str(date.today() + timedelta(days=-7)))
conds.append ("CloseDate <= " + str(date.today()))
conds.append ("StageName IN ('7. Closed Won')")
sf.load_data ('Opportunity', tcols, conds)

for rec in sf.table_data['Opportunity']:
        print (rec)

5 - Querying from a table with matching values (JOIN) in another table

In the example below, the variable jcond is a 3 member array depicting a JOIN criteria

  • 1st Value is the column that needs to be Joined
  • 2nd Value is the Table to join with
  • 3rd Value is the Column in the Join table to match against
from ssutils import Sfdc
sf = Sfdc('userid', 'password', 'token')
sf.connect ()

tcols = ['Id', 'Product2Id', 'TotalPrice', 'OpportunityId', 'CurrencyIsoCode']
conds = ["TotalPrice > 0"]
jcond = ['OpportunityId', 'Opportunity', 'Id']
sf.load_data ('OpportunityLineItem', tcols, conds, jcond)

6 - Limit number of rows in query

In the example below, the query will return maximum 10 rows

from ssutils import Sfdc
sf = Sfdc('userid', 'password', 'token')
sf.connect ()

tcols = ['Id', 'Product2Id', 'TotalPrice', 'OpportunityId', 'CurrencyIsoCode']
conds = ["TotalPrice > 0"]
sf.load_data ('OpportunityLineItem', tcols, conds, jcond, 10)

7 - Add USD Column

In the example below, after table is loaded with 10 rows, a new column called ListPrice_USD is added with amount in US Dollars

from ssutils import Sfdc
sf = Sfdc('userid', 'password', 'token')
sf.connect ()
sf.load_fx_rates ()

table_name  = 'OpportunityLineItem'
tcols = ['Id', 'ListPrice', 'CurrencyIsoCode']
conds = ["TotalPrice > 0"]
sf.load_data ('OpportunityLineItem', tcols, conds, [], 10)
sf.calculate_usd_column (table_name, 'ListPrice',  'CurrencyIsoCode', 'ListPrice_USD')

Date

Use Date to do some calculations on Dates

Examples:

1 - Get Financial Quarter, Month from Date

from ssutils import Date

sd = Date()
print ('Quarter = '         + sd.get_quarter()) # Use current date
print ('Quarter = '         + sd.get_quarter('2019-04-15')) #Use Specified date

print ('Month Full = '      + sd.get_month())
print ('Month Short = '     + sd.get_mon())
print ('WeekNumber = '      + str(sd.get_week_number()))
print ('Year WeekNumber = ' + sd.get_year_week_number())
Output (when run in September 2019):
Quarter = Q3
Quarter = Q2
Month Full = September
Month Short = Sep
WeekNumber = 37
Year WeekNumber = 2019/37

2 - Get Financial Year Bounds from Date

from ssutils import Date

sd = Date()
print ('Finacial Year Start = ' + sd.get_year_start ('2018-04-15'))
print ('Finacial Year End = '   + sd.get_year_end   ('2018-04-15'))
print ('Finacial Year Start = ' + sd.get_year_start ())
print ('Finacial Year End = '   + sd.get_year_end   ())
Output (when run in 2019):
Finacial Year Start = 2018-01-01
Finacial Year End = 2018-12-31
Finacial Year Start = 2019-01-01
Finacial Year End = 2019-12-31

Metadata

Release files for ssutils 0.4.7

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for ssutils 0.4.7
File Size Uploaded Provenance Actions
ssutils-0.4.7.tar.gz 5.7 kB Without provenance Details for ssutils-0.4.7.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for ssutils 0.4.7
File Interpreter ABI Platform Actions
ssutils-0.4.7-py3-none-any.whl Python 3 none any Details for ssutils-0.4.7-py3-none-any.whl

Total release size: 12.7 kB

Release files / ssutils-0.4.7.tar.gz

Download URL ssutils-0.4.7.tar.gz
Size 5.7 kB
Tags Source
SHA-256 checksum
How to use checksums
eaa529b2bccd78237c259b6852dc3731cf9b575ce3ec454b7601d4eb84ee1873
BLAKE2b-256 checksum
How to use checksums
e5ae6b9e615a0720503def30355c1099144dfb055cd9d400321651ee8640343f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.6.6

Release files / ssutils-0.4.7-py3-none-any.whl

Download URL ssutils-0.4.7-py3-none-any.whl
Size 7.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f408a8909e6945cd2bc02910dbb6cb55ce9dfbee14b41123c88ff443c617cac5
BLAKE2b-256 checksum
How to use checksums
72ae8f88a216f934378f904f4a93c89ee4ad8c667dc9e19fb3c6c60b03e3136d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.6.6

Security for ssutils 0.4.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page