Showing posts with label boolean. Show all posts
Showing posts with label boolean. Show all posts

Monday, 29 May 2023

Convert a string to boolean in Python

Using bool() method, we can convert a string to boolean in Python.

 

Example

bool_val = bool(input_str)

string_to_boolean.py

input_str = input('Enter a string ')
print('input_str : ', input_str, " type : ", type(input_str))

bool_val = bool(input_str)
print('bool_val : ', bool_val)

Output

Enter a string hello
input_str :  hello  type :  <class 'str'>
bool_val :  True

Following rules are followed while considering a value as True in Python.

 

a.   Any non-zero numeric value is considered as True. 0, 0.0 are evaluated to False

b.   Nom empty strings, dictionaries, lists, tuples, sets is considered as True. Empty strings (""), empty lists ([]), empty tuples (()) and empty sets ({}) are evaluated as False.

c.    Special value ‘None’ is considered as False

d.   Boolean True is considered as True, and False is considered as False.

 

bool_values.py

print("Following values evaluated to True")
print('bool("True") : ', bool("True"))
print('bool("false") : ', bool("false"))
print('bool("False") : ', bool("False"))
print('bool("Yes") : ', bool("Yes"))
print('bool("No") : ', bool("No"))
print('bool("0") : ', bool("0"))
print('bool("0.0") : ', bool("0.0"))
print('bool("1") : ', bool("1"))
print('bool([2, 3]) : ', bool([2, 3]))
print('bool({"id" : 1}) : ', bool({"id":1}))
print('bool(True) : ', bool(True))

print("\nFollowing values evaluated to False")
print('bool(0) : ', bool(0))
print('bool(0.0) : ', bool(0.0))
print('bool([]) : ', bool([]))
print('bool({}) : ', bool({}))
print('bool(()) : ', bool(()))
print('bool(set()) : ', bool(set()))
print('bool(None) : ', bool(None))
print('bool(False) : ', bool(False))

Output

Following values evaluated to True
bool("True") :  True
bool("false") :  True
bool("False") :  True
bool("Yes") :  True
bool("No") :  True
bool("0") :  True
bool("0.0") :  True
bool("1") :  True
bool([2, 3]) :  True
bool({"id" : 1}) :  True
bool(True) :  True

Following values evaluated to False
bool(0) :  False
bool(0.0) :  False
bool([]) :  False
bool({}) :  False
bool(()) :  False
bool(set()) :  False
bool(None) :  False
bool(False) :  False



 

Previous                                                 Next                                                 Home

Boolean variables in Python

Boolean variable can hold either True or False, and the type ‘bool’ represents a Boolean type.

 

Example

is_raining = True
is_raining = False

Please note that the first letter starts with capital case where T in True, F in False

 

boolean_variables.py

is_raining = True

if is_raining:
    print('It is raining')
else:
    print('It is not raining')

is_raining = False

if is_raining:
    print('It is raining')
else:
    print('It is not raining')


print('type of is_raining : ', type(is_raining))

In the above example, we define a variable ‘is_raining’ to specify whether it is raining or not.

 


Output

It is raining
It is not raining
type of is_raining :  <class 'bool'>


 

 

Previous                                                 Next                                                 Home

Sunday, 13 November 2022

Hibernate 6: Boolean values handling

Hibernate maps boolean values to BOOLEAN column if the underlying database has support for Boolean type. If any database do not have support for BOOLEAN data type, then it use whatever is available like BIT, TINYINT or SMALLINT.

 

To make the things more simple, Hibernate provides three built-in converters to handle common Boolean mapping cases.

 

Converter

Description

NumericBooleanConverter

Map 1 for true, and 0 for false.

TrueFalseConverter

Map ‘T’ for true, and ‘F’ for false.

YesNoConverter

Map ‘Y’ for true, and ‘N’ for false.

 

Example

// this will get mapped to CHAR or NCHAR with a conversion
@Convert(converter = org.hibernate.type.YesNoConverter.class)
Boolean yesNoField;

// this will get mapped to CHAR or NCHAR with a conversion
@Convert(converter = org.hibernate.type.TrueFalseConverter.class)
Boolean trueFalseField;

// this will get mapped to TINYINT with a conversion
@Convert(converter = org.hibernate.type.NumericBooleanConverter.class)
Boolean numericBooleanField;

// this will be mapped to boolean type, if the database has a boolean type, else to some numeric like 
// BIT, TINYINT or SMALLINT.
Boolean implicitField;

 

Follow below step-by-step procedure to build complete working application.

 

Step 1: Create new maven project ‘hibernate-boolean-type-demo’.

 

Step 2: Update pom.xml with maven dependencies.

 

pom.xml

 

<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.sample.app</groupId>
    <artifactId>hibernate-boolean-type-demo</artifactId>
    <version>1</version>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

        <java.version>15</java.version>
        <maven.compiler.source>${java.version}</maven.compiler.source>
        <maven.compiler.target>${java.version}</maven.compiler.target>

    </properties>

    <dependencies>
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>42.4.1</version>
        </dependency>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>6.1.2.Final</version>
        </dependency>

        <dependency>
            <groupId>javax.persistence</groupId>
            <artifactId>javax.persistence-api</artifactId>
            <version>2.2</version>
        </dependency>


    </dependencies>
</project>

 

Step 3: Define BooleanDemo entity class.

 

BooleanDemo.java

 

package com.sample.app.entity;

import jakarta.persistence.Convert;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.Table;

@Entity
@Table(name = "boolean_demo")
public class BooleanDemo {

    @Id
    private Integer id;

    // this will get mapped to CHAR or NCHAR with a conversion
    @Convert(converter = org.hibernate.type.YesNoConverter.class)
    Boolean yesNoField;

    // this will get mapped to CHAR or NCHAR with a conversion
    @Convert(converter = org.hibernate.type.TrueFalseConverter.class)
    Boolean trueFalseField;

    // this will get mapped to TINYINT with a conversion
    @Convert(converter = org.hibernate.type.NumericBooleanConverter.class)
    Boolean numericBooleanField;

    // this will be mapped to boolean type, if the database has a boolean type, else to some numeric like 
    // BIT, TINYINT or SMALLINT.
    Boolean implicitField;

    public BooleanDemo(Integer id, Boolean yesNoField, Boolean trueFalseField, Boolean numericBooleanField,
            Boolean implicitField) {
        this.id = id;
        this.yesNoField = yesNoField;
        this.trueFalseField = trueFalseField;
        this.numericBooleanField = numericBooleanField;
        this.implicitField = implicitField;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public Boolean getYesNoField() {
        return yesNoField;
    }

    public void setYesNoField(Boolean yesNoField) {
        this.yesNoField = yesNoField;
    }

    public Boolean getTrueFalseField() {
        return trueFalseField;
    }

    public void setTrueFalseField(Boolean trueFalseField) {
        this.trueFalseField = trueFalseField;
    }

    public Boolean getNumericBooleanField() {
        return numericBooleanField;
    }

    public void setNumericBooleanField(Boolean numericBooleanField) {
        this.numericBooleanField = numericBooleanField;
    }

    public Boolean getImplicitField() {
        return implicitField;
    }

    public void setImplicitField(Boolean implicitField) {
        this.implicitField = implicitField;
    }

}

Step 4: Create hibernate.cfg.xml file under src/main/resources folder.

 

hibernate.cfg.xml

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>

    <session-factory>

        <!-- Database Connection settings -->
        <property name="connection.driver_class">org.postgresql.Driver</property>
        <property name="connection.url">jdbc:postgresql://127.0.0.1:5432/test</property>
        <property name="connection.username">postgres</property>
        <property name="connection.password">postgres</property>

        <!-- Enable the logging of all the generated SQL statements to the console -->
        <property name="show_sql">true</property>

        <!-- Format the generated SQL statement to make it more readable, -->
        <property name="format_sql">true</property>

        <!-- Hibernate will put comments inside all generated SQL statements to 
            hint what’s the generated SQL trying to do -->
        <property name="use_sql_comments">false</property>

        <property name="dialect">org.hibernate.dialect.PostgreSQLDialect</property>

        <property name="hbm2ddl.auto">update</property>

        <!-- mappings for annotated classes -->
        <mapping class="com.sample.app.entity.BooleanDemo" />

    </session-factory>

</hibernate-configuration>

Step 5: Define main application class.

 

App.java

package com.sample.app;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.Metadata;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;

import com.sample.app.entity.BooleanDemo;

public class App {
    private static final SessionFactory SESSION_FACTORY = buildSessionFactory();

    private static SessionFactory buildSessionFactory() {
        try {

            final StandardServiceRegistry standardRegistry = new StandardServiceRegistryBuilder()
                    .configure("hibernate.cfg.xml").build();

            final Metadata metaData = new MetadataSources(standardRegistry).getMetadataBuilder().build();

            return metaData.getSessionFactoryBuilder().build();

        } catch (Throwable ex) {
            throw new ExceptionInInitializerError(ex);
        }

    }

    public static void main(final String args[]) {

        BooleanDemo p1 = new BooleanDemo(1, true, true, true, true);
        BooleanDemo p2 = new BooleanDemo(2, false, false, false, false);

        try (final Session session = SESSION_FACTORY.openSession()) {
            session.beginTransaction();

            session.persist(p1);
            session.persist(p2);

            session.getTransaction().commit();

        }

    }
}

Total project structure looks like below.




Run App.java, you will see below kind of messages in the console.

Aug 16, 2022 10:01:13 AM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate ORM core version 6.1.2.Final
Aug 16, 2022 10:01:14 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
WARN: HHH10001002: Using built-in connection pool (not intended for production use)
Aug 16, 2022 10:01:14 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001005: Loaded JDBC driver class: org.postgresql.Driver
Aug 16, 2022 10:01:14 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001012: Connecting with JDBC URL [jdbc:postgresql://127.0.0.1:5432/test]
Aug 16, 2022 10:01:14 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001001: Connection properties: {password=****, user=postgres}
Aug 16, 2022 10:01:14 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001003: Autocommit mode: false
Aug 16, 2022 10:01:14 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl$PooledConnections <init>
INFO: HHH10001115: Connection pool size: 20 (min=1)
Aug 16, 2022 10:01:14 AM org.hibernate.engine.jdbc.dialect.internal.DialectFactoryImpl logSelectedDialect
INFO: HHH000400: Using dialect: org.hibernate.dialect.PostgreSQLDialect
Aug 16, 2022 10:01:14 AM org.hibernate.metamodel.internal.EntityInstantiatorPojoStandard resolveConstructor
INFO: HHH000182: No default (no-argument) constructor for class: com.sample.app.entity.BooleanDemo (class must be instantiated by Interceptor)
Aug 16, 2022 10:01:14 AM org.hibernate.resource.transaction.backend.jdbc.internal.DdlTransactionIsolatorNonJtaImpl getIsolatedConnection
INFO: HHH10001501: Connection obtained from JdbcConnectionAccess [org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator$ConnectionProviderJdbcConnectionAccess@4fe64d23] for (non-JTA) DDL execution was not in auto-commit mode; the Connection 'local transaction' will be committed and the Connection will be set into auto-commit mode.
Hibernate: 
    
    create table boolean_demo (
       id integer not null,
        implicitField boolean,
        numericBooleanField integer,
        trueFalseField char(1),
        yesNoField char(1),
        primary key (id)
    )
Aug 16, 2022 10:01:15 AM org.hibernate.engine.transaction.jta.platform.internal.JtaPlatformInitiator initiateService
INFO: HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
Hibernate: 
    insert 
    into
        boolean_demo
        (implicitField, numericBooleanField, trueFalseField, yesNoField, id) 
    values
        (?, ?, ?, ?, ?)
Hibernate: 
    insert 
    into
        boolean_demo
        (implicitField, numericBooleanField, trueFalseField, yesNoField, id) 
    values
        (?, ?, ?, ?, ?)

Query PostgreSQL to confirm the table structure and table content.

test=# \d
            List of relations
 Schema |     Name     | Type  |  Owner   
--------+--------------+-------+----------
 public | boolean_demo | table | postgres
(1 row)

test=# 
test=# \d+ boolean_demo
                                                Table "public.boolean_demo"
       Column        |     Type     | Collation | Nullable | Default | Storage  | Compression | Stats target | Description 
---------------------+--------------+-----------+----------+---------+----------+-------------+--------------+-------------
 id                  | integer      |           | not null |         | plain    |             |              | 
 implicitfield       | boolean      |           |          |         | plain    |             |              | 
 numericbooleanfield | integer      |           |          |         | plain    |             |              | 
 truefalsefield      | character(1) |           |          |         | extended |             |              | 
 yesnofield          | character(1) |           |          |         | extended |             |              | 
Indexes:
    "boolean_demo_pkey" PRIMARY KEY, btree (id)
Access method: heap

test=# 
test=# SELECT * FROM boolean_demo;
 id | implicitfield | numericbooleanfield | truefalsefield | yesnofield 
----+---------------+---------------------+----------------+------------
  1 | t             |                   1 | T              | Y
  2 | f             |                   0 | F              | N
(2 rows)

You can download this application from this link.

 

Previous                                                    Next                                                    Home

Thursday, 11 March 2021

php: is_bool: check variable holds Boolean value or not

Signature

is_bool ( mixed $value ) : bool

 

Description

Finds out whether a variable is a boolean or not. Returns true if value is a bool, false otherwise.

 

boolean_variable_check_demo.php

#!/usr/bin/php

<?php

   $var1 = true;
   $var2 = 123;

   echo "\n\$var1 : ";
   var_dump($var1);

   echo "\$var2 : $var2";

   echo "\n\nis \$var1 boolean : ";
   var_dump(is_bool($var1));

   echo "\nis \$var2 boolean : ";
   var_dump(is_bool($var2));
?>

Output

$./boolean_variable_check_demo.php 


$var1 : bool(true)
$var2 : 123

is $var1 boolean : bool(true)

is $var2 boolean : bool(false)




 

  

Previous                                                    Next                                                    Home