Skip to main content

Java-like input scanner.

Project description

# Overview

PyScanner is a Java-like input scanner.

This is very useful for reading input for programming contests such as ACM ICPC, Topcoder, Google Code Jam, and Facebook Hacker Cup.

# Installation

easy_install PyScanner

And then in your python3 module include:

from scanner import Scanner

# Examples

The following example allows to read a float from stdin:
```python
# Input:
# 3 0.5
sc = Scanner()
x = sc.next_int()
y = sc.next_float()
type(x) is int # True
type(y) is float # True
x + y # 3.5
```

The following code allows to read until EOF and obtain int types:
```python
# Assume input is:
# 10 20 30
# 40 50 60
sc = Scanner()
sum = 0
while sc.has_next():
sum += sc.next_int()
sum # 210
```

The default input stream is sys.stdin. However, it is possible to read from a file or even a string:
```python
sc = Scanner(file='data.txt')
# do stuff
sc.close()
```

```python
sc = Scanner(source='some string to use as input')
```

The scanner can also use string delimeters other than whitespace.
```python
sc = Scanner(delim=',')
```

By default, the scanner does a str split. If forced, a regex pattern can also be used. As expected, the latter method is slower:
```python
content = '1 fish 2.5 fish red fish blue fish
sc = Scanner(source=content, delim='\S*fish\S*', force_regex=True)
sc.next_int() # 1
sc.has_next() # True
sc.next_float() # 2.5
sc.next() # red
sc.next() # blue
sc.has_next() # False
```

# Usage
TODO: example using the Scanner vs. standard Python way.

# License

[THE BEER-WARE LICENSE](https://tldrlegal.com/license/beerware-license)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

PyScanner-1.0.3-py3.3.egg (5.8 kB view details)

Uploaded Egg

File details

Details for the file PyScanner-1.0.3-py3.3.egg.

File metadata

  • Download URL: PyScanner-1.0.3-py3.3.egg
  • Upload date:
  • Size: 5.8 kB
  • Tags: Egg
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for PyScanner-1.0.3-py3.3.egg
Algorithm Hash digest
SHA256 bde3555225c4a12fc22885cb5d65a4eea82ef1be0307a70aef799371fcb6d890
MD5 174653cda36861c584c101c5f522354f
BLAKE2b-256 ee50446a75033832e5d3a3bbc2f20a4fea3cc3de1da5965677563aa440dfe81f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page