8000 gh-106318: Add string method examples by blaisep · Pull Request #119445 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-106318: Add string method examples #119445

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

Draft
wants to merge 67 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
67 commits
Select commit Hold shift + click to select a range
5508700
[Doc] Added snippet code to str.join method
adorilson May 11, 2021
e388d45
[Doc] Added snippet code to str.ljust method
adorilson May 12, 2021
d8407ba
[Doc] Added snippet code to str.rjust method
adorilson May 12, 2021
08b88ef
[Doc] Added 'See also' in str.lstrip and str.rstrip methods from one …
adorilson May 12, 2021
52e3a6b
[Doc] Added snippet code to str.maketrans method
adorilson May 12, 2021
dee059c
[Doc] Added snippet code to str.translate method
adorilson May 12, 2021
1671497
[Doc] Added snippet code to str.partition method
adorilson May 12, 2021
c832623
[Doc] Added snippet code to str.replace method
adorilson May 12, 2021
0be8170
[Doc] Added snippet code to str.rfind method
adorilson May 12, 2021
ff65bd2
[Doc] Added snippet code to str.rindex method
adorilson May 12, 2021
322c88c
[Doc] Added snippet code to str.rpartition method
adorilson May 12, 2021
3a473df
[DOC] minor change
adorilson May 12, 2021
b653693
[Doc] Added snippet code to str.rsplit method
adorilson May 12, 2021
1b412cb
[Doc] Added snippet code to str.startswith method
adorilson May 12, 2021
fb56441
[Doc] Added snippet code to str.swapcase method
adorilson May 12, 2021
04a5212
[Doc] Added snippet code to str.upper method
adorilson May 13, 2021
8771775
[DOC] Added snipped code in str.capitalize
adorilson Feb 27, 2021
f2e224c
[DOC] Add snippet code in str.casefold and str.lower methods
adorilson Feb 27, 2021
1d39f92
[DOC] Added snippet code in str.center method
adorilson Feb 27, 2021
b3a70b6
[DOC] Added snippet code in str.count method
adorilson Feb 27, 2021
37477f8
[DOC] fix typo
adorilson Feb 28, 2021
f9a7293
[DOC] Added snippet code in str.encode method
adorilson Mar 13, 2021
2e54cb0
[DOC] Added snippet code to str.endswith method
adorilson Mar 13, 2021
96d9bf6
[DOC] Added snippet code with \n to str.expandtabs method
adorilson Mar 13, 2021
d1dc7e0
[DOC] Added snippet code to str.index method
adorilson Mar 14, 2021
8661d8e
[DOC] Added snippet code to str.isalnum method
adorilson Mar 14, 2021
9e1f289
[DOC] Added snippet code in str.isalpha method
adorilson Mar 15, 2021
9e361d8
[DOC] Added snippet code in str.isascii method
adorilson Mar 15, 2021
09a463a
[DOC] Added snippet code to str.isdecimal method
adorilson Mar 15, 2021
641de5c
[Doc] Added snippet code to str.isdigit method
adorilson Mar 22, 2021
eae1564
[Doc] Added snippet code to str.islower method
adorilson Mar 22, 2021
58331b8
[Doc] Added snippet code to str.isnumeric method
adorilson Mar 22, 2021
d7ce39c
[Doc] Added snippet code to str.isprintable method
adorilson Mar 22, 2021
b86cefd
[Doc] Added snippet code to str.isspace method
adorilson Mar 22, 2021
3df630c
[Doc] Adde snippet code to str.istitle method
adorilson Mar 22, 2021
ae42fea
Minor changes (formatting and typo)
adorilson Jun 11, 2023
4676daa
[Doc] Making lint happy
adorilson Jun 11, 2023
7642f31
Merge branch 'main' into str_methods_2
adorilson Jul 3, 2023
de11c93
Update Doc/library/stdtypes.rst
adorilson Jul 3, 2023
1695b93
[DOC] Added some 'see also' to str methods
adorilson Jul 3, 2023
ad6b56b
[DOC] Moved 'For example' to the end of the previous paragraph
adorilson Jul 3, 2023
e4e1405
[DOC] Putting a space after # in comments on str methods
adorilson Jul 3, 2023
2dfa3f2
[DOC] Fixed inconsistent literal block quoting
adorilson Jul 3, 2023
27cc327
fix other comments
hauntsaninja Jul 3, 2023
13c01d7
Merge branch 'main' into str_methods_2
adorilson Sep 17, 2023
03bef0a
prepare for FF
blaisep May 22, 2024
9c8955e
update the worflows files
blaisep May 22, 2024
e44d39c
prepare for FF
blaisep May 22, 2024
925c0be
Use the examples created by @adorilson
blaisep May 22, 2024
679eac8
Correct typos in rST formatting
blaisep May 23, 2024
098d9f3
Merge pull request #1 from blaisep/pr-105670
blaisep May 24, 2024
7e7787e
Merge pull request #2 from blaisep/main
blaisep May 24, 2024
031bf40
Apply suggestions from code review
blaisep May 26, 2024
08fcf55
Update stdtypes.rst with edits to content
blaisep May 27, 2024
1c55459
Update unicode.rst
blaisep May 27, 2024
30398db
Update Doc/library/stdtypes.rst
blaisep May 28, 2024
f76afc9
Remove unrelated, conflicting files.
blaisep May 28, 2024
a0d7846
Merge remote-tracking branch 'upstream/main' into pr-105670
blaisep May 28, 2024
c82e92c
Fix trailing space and location of example.
blaisep May 28, 2024
4d70dcf
Adjust the placement of examples more meticulously.
blaisep May 28, 2024
48bed4c
Update Doc/library/stdtypes.rst
blaisep Jun 5, 2024
dce8f2b
Update Doc/library/stdtypes.rst
blaisep Jun 5, 2024
24c1f6a
Update Doc/library/stdtypes.rst
blaisep Jun 5, 2024
fb2fe01
WIP update some examples
blaisep Jun 10, 2024
0beabee
Merge branch 'pr-105670' of github.com:blaisep/cpython into pr-105670
blaisep Jun 10, 2024
02b2cd8
WIP Edit and change some examples
blaisep Jun 11, 2024
0dd8138
Extend the example for str.format_map
blaisep Jul 16, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
Prev Previous commit
Next Next commit
prepare for FF
  • Loading branch information
blaisep committed May 22, 2024
commit 03bef0a859c04dd409644b92331483ff19d0c2d1
61 changes: 61 additions & 0 deletions Doc/c-api/hash.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
.. highlight:: c

PyHash API
----------

See also the :c:member:`PyTypeObject.tp_hash` member.

.. c:type:: Py_hash_t

Hash value type: signed integer.

.. versionadded:: 3.2

.. c:type:: Py_uhash_t

Hash value type: unsigned integer.

.. versionadded:: 3.2


.. c:type:: PyHash_FuncDef

Hash function definition used by :c:func:`PyHash_GetFuncDef`.

.. c::member:: Py_hash_t (*const hash)(const void *, Py_ssize_t)

Hash function.

.. c:member:: const char *name

Hash function name (UTF-8 encoded string).

.. c:member:: const int hash_bits

Internal size of the hash value in bits.

.. c:member:: const int seed_bits

Size of seed input in bits.

.. versionadded:: 3.4


.. c:function:: PyHash_FuncDef* PyHash_GetFuncDef(void)

Get the hash function definition.

.. seealso::
:pep:`456` "Secure and interchangeable hash algorithm".

.. versionadded:: 3.4


.. c:function:: Py_hash_t Py_HashPointer(const void *ptr)

Hash a pointer value: process the pointer value as an integer (cast it to
``uintptr_t`` internally). The pointer is not dereferenced.

The function cannot fail: it cannot return ``-1``.

.. versionadded:: 3.13
230 changes: 230 additions & 0 deletions Doc/howto/timerfd.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,230 @@
.. _timerfd-howto:

*****************************
timer file descriptor HOWTO
*****************************

:Release: 1.13

This HOWTO discusses Python's support for the linux timer file descriptor.


Examples
========

The following example shows how to use a timer file descriptor
to execute a function twice a second:

.. code-block:: python

# Practical scripts should use really use a non-blocking timer,
# we use a blocking timer here for simplicity.
import os, time

# Create the timer file descriptor
fd = os.timerfd_create(time.CLOCK_REALTIME)

# Start the timer in 1 second, with an interval of half a second
os.timerfd_settime(fd, initial=1, interval=0.5)

try:
# Process timer events four times.
for _ in range(4):
# read() will block until the timer expires
_ = os.read(fd, 8)
print("Timer expired")
finally:
# Remember to close the timer file descriptor!
os.close(fd)

To avoid the precision loss caused by the :class:`float` type,
timer file descriptors allow specifying initial expiration and interval
in integer nanoseconds with ``_ns`` variants of the functions.

This example shows how :func:`~select.epoll` can be used with timer file
descriptors to wait until the file descriptor is ready for reading:

.. code-block:: python

import os, time, select, socket, sys

# Create an epoll object
ep = select.epoll()

# In this example, use loopback address to send "stop" command to the server.
#
# $ telnet 127.0.0.1 1234
# Trying 127.0.0.1...
# Connected to 127.0.0.1.
# Escape character is '^]'.
# stop
# Connection closed by foreign host.
#
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.bind(("127.0.0.1", 1234))
sock.setblocking(False)
sock.listen(1)
ep.register(sock, select.EPOLLIN)

# Create timer file descriptors in non-blocking mode.
num = 3
fds = []
for _ in range(num):
fd = os.timerfd_create(time.CLOCK_REALTIME, flags=os.TFD_NONBLOCK)
fds.append(fd)
# Register the timer file descriptor for read events
ep.register(fd, select.EPOLLIN)

# Start the timer with os.timerfd_settime_ns() in nanoseconds.
# Timer 1 fires every 0.25 seconds; timer 2 every 0.5 seconds; etc
for i, fd in enumerate(fds, start=1):
one_sec_in_nsec = 10**9
i = i * one_sec_in_nsec
os.timerfd_settime_ns(fd, initial=i//4, interval=i//4)

timeout = 3
try:
conn = None
is_active = True
while is_active:
# Wait for the timer to expire for 3 seconds.
# epoll.poll() returns a list of (fd, event) pairs.
# fd is a file descriptor.
# sock and conn[=returned value of socket.accept()] are socket objects, not file descriptors.
# So use sock.fileno() and conn.fileno() to get the file descriptors.
events = ep.poll(timeout)

# If more than one timer file descriptors are ready for reading at once,
# epoll.poll() returns a list of (fd, event) pairs.
#
# In this example settings,
# 1st timer fires every 0.25 seconds in 0.25 seconds. (0.25, 0.5, 0.75, 1.0, ...)
# 2nd timer every 0.5 seconds in 0.5 seconds. (0.5, 1.0, 1.5, 2.0, ...)
# 3rd timer every 0.75 seconds in 0.75 seconds. (0.75, 1.5, 2.25, 3.0, ...)
#
# In 0.25 seconds, only 1st timer fires.
# In 0.5 seconds, 1st timer and 2nd timer fires at once.
# In 0.75 seconds, 1st timer and 3rd timer fires at once.
# In 1.5 seconds, 1st timer, 2nd timer and 3rd timer fires at once.
#
# If a timer file descriptor is signaled more than once since
# the last os.read() call, os.read() returns the nubmer of signaled
# as host order of class bytes.
print(f"Signaled events={events}")
for fd, event in events:
if event & select.EPOLLIN:
if fd == sock.fileno():
# Check if there is a connection request.
print(f"Accepting connection {fd}")
conn, addr = sock.accept()
conn.setblocking(False)
print(f"Accepted connection {conn} from {addr}")
ep.register(conn, select.EPOLLIN)
elif conn and fd == conn.fileno():
# Check if there is data to read.
print(f"Reading data {fd}")
data = conn.recv(1024)
if data:
# You should catch UnicodeDecodeError exception for safety.
cmd = data.decode()
if cmd.startswith("stop"):
print(f"Stopping server")
is_active = False
else:
print(f"Unknown command: {cmd}")
else:
# No more data, close connection
print(f"Closing connection {fd}")
ep.unregister(conn)
conn.close()
conn = None
elif fd in fds:
print(f"Reading timer {fd}")
count = int.from_bytes(os.read(fd, 8), byteorder=sys.byteorder)
print(f"Timer {fds.index(fd) + 1} expired {count} times")
else:
print(f"Unknown file descriptor {fd}")
finally:
for fd in fds:
ep.unregister(fd)
os.close(fd)
ep.close()

This example shows how :func:`~select.select` can be used with timer file
descriptors to wait until the file descriptor is ready for reading:

.. code-block:: python

import os, time, select, socket, sys

# In this example, use loopback address to send "stop" command to the server.
#
# $ telnet 127.0.0.1 1234
# Trying 127.0.0.1...
# Connected to 127.0.0.1.
# Escape character is '^]'.
# stop
# Connection closed by foreign host.
#
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.bind(("127.0.0.1", 1234))
sock.setblocking(False)
sock.listen(1)

# Create timer file descriptors in non-blocking mode.
num = 3
fds = [os.timerfd_create(time.CLOCK_REALTIME, flags=os.TFD_NONBLOCK)
for _ in range(num)]
select_fds = fds + [sock]

# Start the timers with os.timerfd_settime() in seconds.
# Timer 1 fires every 0.25 seconds; timer 2 every 0.5 seconds; etc
for i, fd in enumerate(fds, start=1):
os.timerfd_settime(fd, initial=i/4, interval=i/4)

timeout = 3
try:
conn = None
is_active = True
while is_active:
# Wait for the timer to expire for 3 seconds.
# select.select() returns a list of file descriptors or objects.
rfd, wfd, xfd = select.select(select_fds, select_fds, select_fds, timeout)
for fd in rfd:
if fd == sock:
# Check if there is a connection request.
print(f"Accepting connection {fd}")
conn, addr = sock.accept()
conn.setblocking(False)
print(f"Accepted connection {conn} from {addr}")
select_fds.append(conn)
elif conn and fd == conn:
# Check if there is data to read.
print(f"Reading data {fd}")
data = conn.recv(1024)
if data:
# You should catch UnicodeDecodeError exception for safety.
cmd = data.decode()
if cmd.startswith("stop"):
print(f"Stopping server")
is_active = False
else:
print(f"Unknown command: {cmd}")
else:
# No more data, close connection
print(f"Closing connection {fd}")
select_fds.remove(conn)
conn.close()
conn = None
elif fd in fds:
print(f"Reading timer {fd}")
count = int.from_bytes(os.read(fd, 8), byteorder=sys.byteorder)
print(f"Timer {fds.index(fd) + 1} expired {count} times")
else:
print(f"Unknown file descriptor {fd}")
finally:
for fd in fds:
os.close(fd)
sock.close()
sock = None

57 changes: 57 additions & 0 deletions Doc/library/cmdline.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
++++++++++++++++++++++++++++++++++++
Modules command-line interface (CLI)
++++++++++++++++++++++++++++++++++++

The following modules have a command-line interface.

* :ref:`ast <ast-cli>`
* :ref:`asyncio <asyncio-cli>`
* :mod:`base64`
* :ref:`calendar <calendar-cli>`
* :mod:`code`
* :ref:`compileall <compileall-cli>`
* :mod:`cProfile`: see :ref:`profile <profile-cli>`
* :ref:`difflib <difflib-interface>`
* :ref:`dis <dis-cli>`
* :mod:`doctest`
* :mod:`!encodings.rot_13`
* :mod:`ensurepip`
* :mod:`filecmp`
* :mod:`fileinput`
* :mod:`ftplib`
* :ref:`gzip <gzip-cli>`
* :ref:`http.server <http-server-cli>`
* :mod:`!idlelib`
* :ref:`inspect <inspect-module-cli>`
* :ref:`json.tool <json-commandline>`
* :mod:`mimetypes`
* :mod:`pdb`
* :mod:`pickle`
* :ref:`pickletools <pickletools-cli>`
* :mod:`platform`
* :mod:`poplib`
* :ref:`profile <profile-cli>`
* :mod:`pstats`
* :ref:`py_compile <py_compile-cli>`
* :mod:`pyclbr`
* :mod:`pydoc`
* :mod:`quopri`
* :mod:`runpy`
* :ref:`site <site-commandline>`
* :ref:`sqlite3 <sqlite3-cli>`
* :ref:`sysconfig <sysconfig-cli>`
* :mod:`tabnanny`
* :ref:`tarfile <tarfile-commandline>`
* :mod:`!this`
* :ref:`timeit <timeit-command-line-interface>`
* :ref:`tokenize <tokenize-cli>`
* :ref:`trace <trace-cli>`
* :mod:`turtledemo`
* :ref:`unittest <unittest-command-line-interface>`
* :ref:`uuid <uuid-cli>`
* :mod:`venv`
* :mod:`webbrowser`
* :ref:`zipapp <zipapp-command-line-interface>`
* :ref:`zipfile <zipfile-commandline>`

See also the :ref:`Python command-line interface <using-on-general>`.
39 changes: 39 additions & 0 deletions Include/cpython/pyhash.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#ifndef Py_CPYTHON_HASH_H
# error "this header file must not be included directly"
#endif

/* Prime multiplier used in string and various other hashes. */
#define _PyHASH_MULTIPLIER 1000003UL /* 0xf4243 */

/* Parameters used for the numeric hash implementation. See notes for
_Py_HashDouble in Python/pyhash.c. Numeric hashes are based on
reduction modulo the prime 2**_PyHASH_BITS - 1. */

#if SIZEOF_VOID_P >= 8
# define _PyHASH_BITS 61
#else
# define _PyHASH_BITS 31
#endif

#define _PyHASH_MODULUS (((size_t)1 << _PyHASH_BITS) - 1)
#define _PyHASH_INF 314159
#define _PyHASH_IMAG _PyHASH_MULTIPLIER

/* Helpers for hash functions */
PyAPI_FUNC(Py_hash_t) _Py_HashDouble(PyObject *, double);

// Kept for backward compatibility
#define _Py_HashPointer Py_HashPointer


/* hash function definition */
typedef struct {
Py_hash_t (*const hash)(const void *, Py_ssize_t);
const char *name;
const int hash_bits;
const int seed_bits;
} PyHash_FuncDef;

PyAPI_FUNC(PyHash_FuncDef*) PyHash_GetFuncDef(void);

PyAPI_FUNC(Py_hash_t) Py_HashPointer(const void *ptr);
Loading
0