arundhaj

all that is technology

Gambler's Ruin problem simulation

 

Detailed problem statement can be found here Gambler's Ruin

General solution to the problem is;

\(1- a_1 = a_1 \sum\limits_{i = 1}^{k-1} \left(\frac{1 - p}{p}\right)^i\)

For a fair game of \(p = \frac{1}{2}\), \(a_1 = \frac{1}{k}\)

and, for an unfair game of \(p …

Python f-string

 

f-strings is a short for "formatted strings", provides a convenient way to embed expressions inside string literals. This supports more formatting options than the old str.format() method supports.

Few examples;

String operation

>>> name = 'Arunkumar'
>>> f'My name is {name.upper()}'
'My name is ARUNKUMAR'

Float formatting

>>> import math
>>> f …

Setting content-type for files uploaded to S3

 

While uploading files to AWS S3 using Python Boto3 library, it would by default set the content-type as binary. Below snippet would set the appropriate content type based on the file extension.

import mimetypes
...
file_mime_type, _ = mimetypes.guess_type(filename)
...
bucket_obj.upload_file(local_path, s3_path, ExtraArgs={'ContentType': file_mime_type})
...

The above snippet would …

Syncing files to AWS S3 bucket using AWS CLI

 

AWS CLI is a command line tool used for interacting with AWS services.

In this post, I'll share the command to sync your local files to S3 bucket by removing files in the bucket that are not present in the local folder.

$ aws s3 sync . s3://bucket-name --delete

--delete option …

Securing your webapp with AWS Cognito

 

AWS Cognito provides authentication, authorization, and user management for your webapps. In this article I will show Angular snippets to perform authentication with AWS Cognito credentials.

First, I'll show the CognitoService class with just signIn functionality. I've removed other operations like register, confirm registration, etc. from the snippet for simplicity …

Read and Write DataFrame from Database using PySpark

 

To load a DataFrame from a MySQL table in PySpark

source_df = sqlContext.read.format('jdbc').options(
          url='jdbc:mysql://localhost/database_name',
          driver='com.mysql.jdbc.Driver',
          dbtable='SourceTableName',
          user='your_user_name',
          password='your_password').load()

And to write a DataFrame to a MySQL table

destination_df.write.format('jdbc').options(
          url='jdbc:mysql …

Calculate difference with previous row in PySpark

 

To find the difference between the current row value and the previous row value in spark programming with PySpark is as below

Let say, we have the following DataFrame and we shall now calculate the difference of values between consecutive rows.

+---+-----+
| id|value|
+---+-----+
|  1|   65|
|  2|   66|
|  3|   65|
|  4 …

Getting started with AWS Kinesis using Python

 

Amazon Kinesis is a fully managed stream hosted on AWS. It is used to collect and process large streams of data in real time. Along with Kinesis Analytics, Kinesis Firehose, AWS Lambda, AWS S3, AWS EMR you can build a robust distributed application to power your real-time monitoring dashboards, do …